Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Mar 4, 2011, 09:51 AM   #1
l0uismustdie
macrumors member
 
Join Date: Nov 2009
Location: Edinburgh, UK
Can't execute java from command line

Hello, I am attempting to run some java from my terminal and seems to be getting the following error:
Code:
09:44@joshMac:.+rc/edu/american$ java DataLink
Exception in thread "main" java.lang.NoClassDefFoundError: DataLink
Caused by: java.lang.ClassNotFoundException: DataLink
	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:307)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Upon reading some stuff online I found that this is an error with the java CLASSPATH. So, I tried that following:
Code:
09:46@joshMac:.+rc/edu/american$ java DataLink -cp .
Which has the same result.

Also, when checking the classpath:
Code:
09:50@joshMac:.+t1/edu/american$ pwd
/Users/***/Documents/school/11spr/csc546/project1/edu/american
09:50@joshMac:.+t1/edu/american$ $CLASSPATH
-bash: /Users/***/Documents/school/11spr/csc435/project/edu/american: No such file or directory
This is very odd because I have run tons of java code from my terminal so I'm not sure what could be going on here.

Any help would be much appreciated. Thanks.
l0uismustdie is offline   0 Reply With Quote
Old Mar 4, 2011, 11:04 AM   #2
lesterjune
macrumors newbie
 
Join Date: May 2009
Quote:
Originally Posted by l0uismustdie View Post
Hello, I am attempting to run some java from my terminal and seems to be getting the following error:
Code:
09:44@joshMac:.+rc/edu/american$ java DataLink
Exception in thread "main" java.lang.NoClassDefFoundError: DataLink
Caused by: java.lang.ClassNotFoundException: DataLink
	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:307)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Upon reading some stuff online I found that this is an error with the java CLASSPATH. So, I tried that following:
Code:
09:46@joshMac:.+rc/edu/american$ java DataLink -cp .
Which has the same result.

Also, when checking the classpath:
Code:
09:50@joshMac:.+t1/edu/american$ pwd
/Users/***/Documents/school/11spr/csc546/project1/edu/american
09:50@joshMac:.+t1/edu/american$ $CLASSPATH
-bash: /Users/***/Documents/school/11spr/csc435/project/edu/american: No such file or directory
This is very odd because I have run tons of java code from my terminal so I'm not sure what could be going on here.

Any help would be much appreciated. Thanks.
In which directory did you execute your command? And is your DataLink class part of a package?
lesterjune is offline   0 Reply With Quote
Old Mar 4, 2011, 12:36 PM   #3
l0uismustdie
Thread Starter
macrumors member
 
Join Date: Nov 2009
Location: Edinburgh, UK
I executed the javac and java commands from within the same directory as the pwd printed (/Users/***/Documents/school/11spr/csc546/edu/american)

It is part of a package and that could be my problem because that is really the only thing different between this and what I've done in the past.
l0uismustdie is offline   0 Reply With Quote
Old Mar 4, 2011, 01:34 PM   #4
naples98
macrumors member
 
Join Date: Sep 2008
Location: Houston
Quote:
Originally Posted by l0uismustdie View Post
I executed the javac and java commands from within the same directory as the pwd printed (/Users/***/Documents/school/11spr/csc546/edu/american)

It is part of a package and that could be my problem because that is really the only thing different between this and what I've done in the past.
You will have to run the java command from the package directory. For example, if your package folder was "package" and your class was "MyClass" then you would have to run the following command inside the package directory

Code:
java package.MyClass
naples98 is offline   0 Reply With Quote
Old Mar 4, 2011, 02:46 PM   #5
l0uismustdie
Thread Starter
macrumors member
 
Join Date: Nov 2009
Location: Edinburgh, UK
Still no luck. If this is what you meant. My package is edu.american. So, I tried the following:
Code:
14:41@joshMac:.+t1/edu/american$ pwd
/Users/***/Documents/school/11spr/csc546/project1/edu/american
14:41@joshMac:.+t1/edu/american$ java edu.american.DataLink
Exception in thread "main" java.lang.NoClassDefFoundError: edu/american/DataLink
Caused by: java.lang.ClassNotFoundException: edu.american.DataLink
	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:307)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Is that what you meant?

One thing to note is these are all symbolic links
Code:
-rw-r--r--  1 ***  ***   853 Mar  3 19:27 DataLink$ButtonListener.class
-rw-r--r--  1 ***  ***  2422 Mar  3 19:27 DataLink.class
lrwxr-xr-x  1 ***  ***    85 Feb 24 22:17 DataLink.java -> /Users/***/EclipseProjects/SlidingWindowProtocol/src/edu/american/DataLink.java
-rw-r--r--  1 ***  ***  6249 Mar  3 19:27 DataLinkHost.class
lrwxr-xr-x  1 ***  ***    89 Feb 24 22:18 DataLinkHost.java -> /Users/***/EclipseProjects/SlidingWindowProtocol/src/edu/american/DataLinkHost.java
-rw-r--r--  1 ***  ***  1454 Mar  3 19:27 Wire.class
lrwxr-xr-x  1 ***  ***    81 Feb 24 22:18 Wire.java -> /Users/***/EclipseProjects/SlidingWindowProtocol/src/edu/american/Wire.java
However, I have tried running all these commands from both locations.
l0uismustdie is offline   0 Reply With Quote
Old Mar 4, 2011, 02:50 PM   #6
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
As per normal, standard, Java rules the .class files must be in a folder hierarchy that matches the reverse DNS package.

So in this case they should be in edu/american/ relative to the current path (or relative to any path in the CLASSPATH).
robbieduncan is offline   0 Reply With Quote
Old Mar 4, 2011, 03:06 PM   #7
RaceTripper
macrumors 68030
 
RaceTripper's Avatar
 
Join Date: May 2007
you need to have a directory structure along the lines of

Code:
.../build/edu/american/DataLink.class
.../build/edu/american/etc (other class files in package
Then you can execute your java command from the build directory, e.g.

Code:
/Users/***/Documents/school/11spr/csc546/build $ java edu.american.DataLink
Your classpath needs to point to the directory that roots your package directory structure. So for the path: /Users/***/Documents/school/11spr/csc546/build/edu/american/DataLink.class

Your classpath entry needs to be: /Users/***/Documents/school/11spr/csc546/build

If you are executing java from build/ you are set since . is automatically part of the classpath (unless you set the CLASSPATH environment variable otherwise).

If you are not executing from build, then you need to set the classpath explicitly:

Code:
java -cp /Users/***/Documents/school/11spr/csc546/build edu.american.DataLink
RaceTripper is offline   0 Reply With Quote
Old Mar 4, 2011, 03:12 PM   #8
l0uismustdie
Thread Starter
macrumors member
 
Join Date: Nov 2009
Location: Edinburgh, UK
Yeah I am good to go. On top of all that I forgot I had set a classpath in my .bashrc for something...

Thanks!!
l0uismustdie is offline   0 Reply With Quote
Old Mar 4, 2011, 03:12 PM   #9
RaceTripper
macrumors 68030
 
RaceTripper's Avatar
 
Join Date: May 2007
Quote:
Originally Posted by robbieduncan View Post
As per normal, standard, Java rules the .class files must be in a folder hierarchy that matches the reverse DNS package.

So in this case they should be in edu/american/ relative to the current path (or relative to any path in the CLASSPATH).
It's more correct to say the directory structure should match the package hierarchy for defined classes. Using reverse DNS is nothing more than a convention.

So for package a.b.c.d there should be a corresponding directory structure for source files and class files (they don't have to be the same ones), e.g.
src/a/b/c/d
build/a/b/c/d
RaceTripper is offline   0 Reply With Quote
Old Mar 4, 2011, 03:14 PM   #10
RaceTripper
macrumors 68030
 
RaceTripper's Avatar
 
Join Date: May 2007
Quote:
Originally Posted by l0uismustdie View Post
Yeah I am good to go. On top of all that I forgot I had set a classpath in my .bashrc for something...

Thanks!!
CLASSPATH environment variable set globally is evil. Don't do it. It will cause you nothing but grief. Use projects scripts (better yet, use Apache Ant or Maven) to set classpath locally for each project.
RaceTripper is offline   0 Reply With Quote
Old Feb 7, 2013, 12:26 PM   #11
jhuerta50
macrumors newbie
 
Join Date: Feb 2013
Quote:
Originally Posted by RaceTripper View Post
you need to have a directory structure along the lines of

Code:
.../build/edu/american/DataLink.class
.../build/edu/american/etc (other class files in package
Then you can execute your java command from the build directory, e.g.

Code:
/Users/***/Documents/school/11spr/csc546/build $ java edu.american.DataLink
Your classpath needs to point to the directory that roots your package directory structure. So for the path: /Users/***/Documents/school/11spr/csc546/build/edu/american/DataLink.class

Your classpath entry needs to be: /Users/***/Documents/school/11spr/csc546/build

If you are executing java from build/ you are set since . is automatically part of the classpath (unless you set the CLASSPATH environment variable otherwise).

If you are not executing from build, then you need to set the classpath explicitly:

Code:
java -cp /Users/***/Documents/school/11spr/csc546/build edu.american.DataLink

I realize this is an old post that I found. But for other new users out there, here is what I was able to do to get the java command to actually run the .class file.

I was able to cd to the directory of my .java and .class files and run both the javac and java cammands. Once I ran the java command, my program popped up an input window just as it should have.

So my terminal prompt began at (systemname:~ user$) and since my Java files are in a folder called JavaFiles, I typed this to change directory to my file location. (cd JavaFiles/Assn2) without the parenthesis of coarse.

then I was able to run both the javac and the java commands just fine without getting the Exception error when running the java command.
jhuerta50 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
General: iOS7 command line... Equivalent of "say" command? RobertoG Jailbreaks and iOS Hacks 0 Jan 19, 2014 06:05 PM
Run Basic Command-line Java Programs in Sandvox jblagden Mac Applications and Mac App Store 0 Oct 2, 2013 03:23 PM
Local Java Applications Don't Execute AppleSJ511606 OS X 10.8 Mountain Lion 0 Apr 20, 2013 10:51 AM
Need Command-line help EricBrian Community Discussion 1 Feb 20, 2013 09:40 PM
How to make a app to execute terminal command DavidBlack Mac Programming 6 Feb 3, 2013 05:41 PM

Forum Jump

All times are GMT -5. The time now is 11:02 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC