New To Mac-Forums?

Welcome to our community! Join the discussion today by registering your FREE account. If you have any problems with the registration process, please contact us!

Get your questions answered by community gurus Advice and insight from world-class Apple enthusiasts Exclusive access to members-only contests, giveaways and deals

Join today!

 
Start a Discussion
 

Mac-Forums Brief

Subscribe to Mac-Forums Brief to receive special offers from Mac-Forums partners and sponsors

Join the conversation RSS
OS X - Development and Darwin Discussion and questions about development for Mac OS X.

Learning java on the mac, need help with error


Post Reply New Thread Subscribe

 
Thread Tools
Alejandro Lanza

 
Member Since: Jan 22, 2012
Posts: 5
Alejandro Lanza is on a distinguished road

Alejandro Lanza is offline
Hello,

First post here and looking forward to exchanging good knowledge. I've decided to learn to program and have started with java, i'm using this book here: Amazon.com: Java, A Beginner's Guide, 5th Edition (9780071606325): Herbert Schildt: Books . I have a 2007 macbook that also loads xp through bootcamp and also virtualbox and haven't had issues doing the examples of the book on that OS.
However i can't run even the first example of the book on OS X (10.6.8). I'm able to compile it:

Code:
/*
   This is a simple Java program.

   Call this file Example.java.
*/
class Example {
  // A Java program begins with a call to main().
  public static void main(String args[]) {
    System.out.println("Java drives the Web.");
  }
}
the error i get is:

Code:
Ales-MacBook:~ MacBook$ java /Users/MacBook/Documents/Java_coding/Example
Exception in thread "main" java.lang.NoClassDefFoundError: /Users/MacBook/Documents/Java_coding/Example
Caused by: java.lang.ClassNotFoundException: .Users.MacBook.Documents.Java_coding.Example
	at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
My machine is running java se 6.
Thoughts on how to fix this?
Thanks in advance!!

Alejandro
QUOTE Thanks
vansmith

 
vansmith's Avatar
 
Member Since: Oct 19, 2008
Location: Toronto
Posts: 18,352
vansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond repute
Mac Specs: 2012 13" MBP (2.5 i5, 8GB)

vansmith is offline
Compiles and runs fine here. That's also the most basic app you could possibly write so it's not as if it's some fancy feature causing problems (you would have found out if this was the case at compile time).

The java.lang.NoClassDefFoundError exception is thrown when the class you specified isn't found. In other words, there's no Example.class file in /Users/MacBook/Documents/Java_coding/. Check your directories and try again.

Important Links: Community Guidelines : Use the reputation system if you've been helped.
M-F Blog :: Write for the blog
Writing a Quality Post
QUOTE Thanks
Alejandro Lanza

 
Member Since: Jan 22, 2012
Posts: 5
Alejandro Lanza is on a distinguished road

Alejandro Lanza is offline
Strange, i know the file is there. In fact i ran this to compile it:

Code:
Ales-MacBook:~ MacBook$ javac /Users/MacBook/Documents/Java_coding/Example.java

Ales-MacBook:~ MacBook$ java /Users/MacBook/Documents/Java_coding/Example.class

Exception in thread "main" java.lang.NoClassDefFoundError: /Users/MacBook/Documents/Java_coding/Example/class
Caused by: java.lang.ClassNotFoundException: .Users.MacBook.Documents.Java_coding.Example.class
	at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Ales-MacBook:~ MacBook$
and that in fact produced the Example.class file as you can see above, but for some reason i can't run the program afterwards
Thanks for the advice Vansmith!
QUOTE Thanks
vansmith

 
vansmith's Avatar
 
Member Since: Oct 19, 2008
Location: Toronto
Posts: 18,352
vansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond repute
Mac Specs: 2012 13" MBP (2.5 i5, 8GB)

vansmith is offline
You need to exclude the .class extension when using java to run the application. I don't know why that's a requirement but it is.

What version of Java are you using? You can find this by running the following:
Code:
java -version

Important Links: Community Guidelines : Use the reputation system if you've been helped.
M-F Blog :: Write for the blog
Writing a Quality Post
QUOTE Thanks
Alejandro Lanza

 
Member Since: Jan 22, 2012
Posts: 5
Alejandro Lanza is on a distinguished road

Alejandro Lanza is offline
Thanks, you're right i should have removed that as per the book, but even without it i still get the error:

Code:
Ales-MacBook:~ MacBook$ java /Users/MacBook/Documents/Java_coding/Example
Exception in thread "main" java.lang.NoClassDefFoundError: /Users/MacBook/Documents/Java_coding/Example
Caused by: java.lang.ClassNotFoundException: .Users.MacBook.Documents.Java_coding.Example
	at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Ales-MacBook:~ MacBook$ java -version
java version "1.6.0_29"
Java(TM) SE Runtime Environment (build 1.6.0_29-b11-402-10M3527)
Java HotSpot(TM) Client VM (build 20.4-b02-402, mixed mode)
Ales-MacBook:~ MacBook$
Thanks again!
QUOTE Thanks
vansmith

 
vansmith's Avatar
 
Member Since: Oct 19, 2008
Location: Toronto
Posts: 18,352
vansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond repute
Mac Specs: 2012 13" MBP (2.5 i5, 8GB)

vansmith is offline
Ok, the solution is quite simple. First, "cd" into the directory and then execute java:
Code:
cd /Users/MacBook/Documents/Java_coding/
java Example
That should work. For some reason, java likes to be in the directory of the class (something to do with classpaths...my Java knowledge is weak).

This is why I hate Java and love Python.

Important Links: Community Guidelines : Use the reputation system if you've been helped.
M-F Blog :: Write for the blog
Writing a Quality Post
QUOTE Thanks
Alejandro Lanza

 
Member Since: Jan 22, 2012
Posts: 5
Alejandro Lanza is on a distinguished road

Alejandro Lanza is offline
Oh wow, understood that worked perfect
Thank you so much Vansmith!!
QUOTE Thanks
tonk

 
Member Since: Jan 25, 2012
Posts: 14
tonk is on a distinguished road

tonk is offline
Well, since issue resolved, just a recommendation: why won't you code java in eclipse ? for java it is probably the best IDE out there (C++ is a little weird on it though).

Eclipse is free and very powerful + its very good for both the beginners and experienced users.

Compiling programs through terminal and writing programs in text editors, just seems like extra work and quite uncomfortable (IMHO).
QUOTE Thanks
vansmith

 
vansmith's Avatar
 
Member Since: Oct 19, 2008
Location: Toronto
Posts: 18,352
vansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond repute
Mac Specs: 2012 13" MBP (2.5 i5, 8GB)

vansmith is offline
You could make the opposite argument that Eclipse is bloated and overkill for most novice programmers. I used it for a while for Python development only to return to a nice simple text editor. It's all a matter of preference I suppose.

Important Links: Community Guidelines : Use the reputation system if you've been helped.
M-F Blog :: Write for the blog
Writing a Quality Post
QUOTE Thanks
tonk

 
Member Since: Jan 25, 2012
Posts: 14
tonk is on a distinguished road

tonk is offline
hm, perhaps. To be honest I've tried very few IDE for java - the two that really pop in mind is eclipse and netbeans. But I strongly agree with you - it is a matter of preference I guess my point was that coding with IDE is better than writing in a text-editor and compiling it through terminal, which is what OP is doing.
QUOTE Thanks
Alejandro Lanza

 
Member Since: Jan 22, 2012
Posts: 5
Alejandro Lanza is on a distinguished road

Alejandro Lanza is offline
Thanks for the suggestion Tonk. I had it in the back of my mind to test netbeans (wasn't aware of eclipse), but at this point in time i had not felt the need to do so. To be honest i didn't know what an IDE was until a few days ago but my reasoning at this point is to focus on what this book teaches and keep the rest as simple as possible.
And Vansmith thanks for the input on this subject too!
QUOTE Thanks
vansmith

 
vansmith's Avatar
 
Member Since: Oct 19, 2008
Location: Toronto
Posts: 18,352
vansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond repute
Mac Specs: 2012 13" MBP (2.5 i5, 8GB)

vansmith is offline
An IDE might be a nice addition to your process. They aren't overly complicated (at least not the basic stuff) and you will get simple access to the JDK through a GUI along with project management. It might not hurt to take a look since NetBeans and Eclipse are free.

Important Links: Community Guidelines : Use the reputation system if you've been helped.
M-F Blog :: Write for the blog
Writing a Quality Post
QUOTE Thanks
tonk

 
Member Since: Jan 25, 2012
Posts: 14
tonk is on a distinguished road

tonk is offline
No worries, just the thing is - it might be easy to write simple stuff in text editor, but it will be a hassle to write slightly more complicated stuff that involves multiple classes. I mean, when you're gonna have 20 classes or more -> have fun with plain text editors and compilers in terminal Thus, why not spend a little time now and learn about about IDE and start getting use to it? If you're decide to go further into development - that will save you so much time in future. If all you want to write simple stuff just for the fun of it -> yeah, you don't need to worry about IDE at all then. Again, IMHO
QUOTE Thanks
vansmith

 
vansmith's Avatar
 
Member Since: Oct 19, 2008
Location: Toronto
Posts: 18,352
vansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond reputevansmith has a reputation beyond repute
Mac Specs: 2012 13" MBP (2.5 i5, 8GB)

vansmith is offline
Absolutely. Managing projects with multiple source files can be tedious in a simple text editor and that will be where an IDE shines. For a beginner though, it might be too complex (especially if the book doesn't use one).

However, as we both noted, it's all a matter of preference.

Important Links: Community Guidelines : Use the reputation system if you've been helped.
M-F Blog :: Write for the blog
Writing a Quality Post
QUOTE Thanks
tonk

 
Member Since: Jan 25, 2012
Posts: 14
tonk is on a distinguished road

tonk is offline
True to that
QUOTE Thanks

Post Reply New Thread Subscribe


« RE: make, xcode and the such | AppleScript - Tell iTunes to use specific AppleTV »
Thread Tools

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread
Thread Starter
Forum
Replies
Last Post
Time Machine never finishes? rsales Other Hardware and Peripherals 8 01-26-2011 06:45 PM
Mac Songs The mac 13 Switcher Hangout 7 05-06-2010 01:35 AM
Need to know how much RAM your Mac can hold? iLaugh Other Hardware and Peripherals 6 12-13-2009 02:49 PM
Error Code 36 ifanyonecan Other Hardware and Peripherals 6 08-22-2008 01:13 AM
Mac Users Get More ISP Choices schweb Apple Rumors and Reports 1 03-06-2003 11:57 AM

All times are GMT -4. The time now is 08:22 PM.

Powered by vBulletin
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
X

Welcome to Mac-Forums.com

Create your username to jump into the discussion!

New members like you have made this community the ultimate source for your Mac since 2003!


(4 digit year)

Already a member?