Can't execute java from command line

Discussion in 'Mac Programming' started by l0uismustdie, Mar 4, 2011.

  1. macrumors regular

    Joined:
    Nov 30, 2009
    Location:
    Edinburgh, UK
    #1
    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.
     
  2. macrumors newbie

    Joined:
    May 12, 2009
    #2
    In which directory did you execute your command? And is your DataLink class part of a package?
     
  3. thread starter macrumors regular

    Joined:
    Nov 30, 2009
    Location:
    Edinburgh, UK
    #3
    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.
     
  4. macrumors member

    naples98

    Joined:
    Sep 9, 2008
    Location:
    Houston
    #4
    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 
     
  5. thread starter macrumors regular

    Joined:
    Nov 30, 2009
    Location:
    Edinburgh, UK
    #5
    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.
     
  6. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #6
    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).
     
  7. macrumors 68030

    RaceTripper

    Joined:
    May 29, 2007
    #7
    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
     
  8. thread starter macrumors regular

    Joined:
    Nov 30, 2009
    Location:
    Edinburgh, UK
    #8
    Yeah I am good to go. On top of all that I forgot I had set a classpath in my .bashrc for something...

    Thanks!!
     
  9. macrumors 68030

    RaceTripper

    Joined:
    May 29, 2007
    #9
    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
     
  10. macrumors 68030

    RaceTripper

    Joined:
    May 29, 2007
    #10
    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.
     
  11. macrumors newbie

    Joined:
    Feb 7, 2013
    #11

    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.
     

Share This Page