Results 1 to 11 of 11
  1. #1
    DerekRaimann is offline Member
    Join Date
    Dec 2010
    Posts
    45
    Rep Power
    0

    Question Classpath problem for current directory

    I am working in a directory called c:\j21work. I created a class file called PrimeFinder.class that contains a class definition and another class file called PrimeThreads.class which contains a class and main() method.

    I am receiving the error:
    Exception in thread "main" java.lang.NoClassDefFoundError: PrimeThreads/java

    After researching it, this is apparently due to needing the specify that the current path be used in the CLASSPATH environment variable. I checked the variable, and the very first character of the CLASSPATH is "." followed by a ";" and all of the other paths included in the classpath.

    I am frustrated because that is supposed to be all the classpath needs to compile properly, allowing java to look at the current directory and find the file it needs to find, which is PrimeFinder.class, but it does not.

    If I use the command line: java -classpath . PrimeThreads, it works fine, but I don't want to have to use the -classpath argument to run my class files, it just means that I don't have the proper configuration in CLASSPATH.

    So I went ahead and added C:\j21work as a path in CLASSPATH, and although it works, it really isn't the same as having the current directory checked. With this fix, it means that every time I want to work in a new directory and create java class files in it, I have to specify that directory as well in the CLASSPATH environment variable, or place that directory as a subdirectory to an already specified directory in the classpath.

    I am just trying to do things the right way and configure java properly so it is more efficient and I don't make more work for myself and more configuration changes than necessary. I'm sorry if this was a lengthy post, but I would really love to solve this problem. Thanks a lot for reading this and taking the time to help me out, I really do appreciate it! :)

    -Derek Raimann

    "Your head asplode!"

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    I am receiving the error:
    Exception in thread "main" java.lang.NoClassDefFoundError: PrimeThreads/java

    What is the command you used that results in this error? (Copy and post the command and the runtime output message.)

    -----------------

    This has nothing to do with the CLASSPATH environment variable, or the classpath being used by the runtime.

    The command

    Java Code:
    java -classpath . PrimeThreads

    is a good - and, as you've found, effective - way of running the program. Whether or not you want to use the -classpath argument it is available and it works as documented. On the other hand adding C:\j21work to the CLASSPATH environment variable seems a little restricting unless you intend always using that particular directory for your classes.

    But, to repeat, the particular NoClassDefFound error you posted was not a problem with the classpath. Post the command that yielded this message.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by DerekRaimann View Post
    After researching it, this is apparently due to needing the specify that the current path be used in the CLASSPATH environment variable. I checked the variable, and the very first character of the CLASSPATH is "." followed by a ";" and all of the other paths included in the classpath.
    I don't know that how you search and where. But the easiest way to confirm that the classpath is setup correctly is by typing the command javac on your command prompt. Did you do that?

  4. #4
    DerekRaimann is offline Member
    Join Date
    Dec 2010
    Posts
    45
    Rep Power
    0

    Smile

    Strangely enough, I decided to take the C:\j21work directory out of the CLASSPATH environment variable and recompile the PrimeThreads.java class. I ran the class: javac PrimeThreads 1 10 100 1000, and it worked perfectly. I am hoping that the system isn't waiting for a restart to refresh the CLASSPATH and that it is working with the current directory as it should. I'll keep you posted if I run into further problems! Thank you for your replies, hopefully the exception errors were just a strange occurrence that won't happen again!

    -Derek Raimann

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    just a strange occurrence

    Or an occurrence entirely consistent with my observation that the posted error message had nothing to do with the classpath. (Or the CLASSPATH environment variable.)

    Sorry to be tetchy, but my request to see the actual command and the runtime output that it led to (copied and posted exactly as they appeared on the screen) was a reasonable one, I think.
    Last edited by pbrockway2; 12-07-2010 at 08:32 AM.

  6. #6
    DerekRaimann is offline Member
    Join Date
    Dec 2010
    Posts
    45
    Rep Power
    0

    Default

    I'm sorry, I got a little too excited when I saw the program work without the classpath c:\j21work, and way overdue for sleep. I didn't mean to bypass your request for the command and runtime output, so here it is:

    java PrimeThreads.java 1 10 100 1000
    Exception in thread "main" java.lang.NoClassDefFoundError: PrimeThreads/java
    Caused by: java.lang.ClassNotFoundException: PrimeThreads.java
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    Could not find the main class: PrimeThreads.java. Program will exit.

    Any idea what those errors were aiming at? It didn't specify any line number that the error occurred, just these preceding packages and methods. Thanks again and sorry again for not posting this last time I posted!

    -Derek Raimann

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    java PrimeThreads.java 1 10 100 1000

    The problem is that you specified a java source file (PrimeThreads.java) and not a class (PrimeThreads). The runtime was looking for a class called java in a package called PrimtThreads. The command of course should have been:

    Java Code:
    java PrimeThreads 1 10 100 1000

    We've all made (and make!) that mistake.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by DerekRaimann View Post
    Strangely enough, I decided to take the C:\j21work directory out of the CLASSPATH environment variable and recompile the PrimeThreads.java class. I ran the class: javac PrimeThreads 1 10 100 1000, and it worked perfectly. I am hoping that the system isn't waiting for a restart to refresh the CLASSPATH and that it is working with the current directory as it should. I'll keep you posted if I run into further problems! Thank you for your replies, hopefully the exception errors were just a strange occurrence that won't happen again!

    -Derek Raimann
    Are you sure that you haven't miss any ; between?

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by pbrockway2 View Post
    We've all made (and make!) that mistake.
    Oh yes, I made it too. :o

  10. #10
    DerekRaimann is offline Member
    Join Date
    Dec 2010
    Posts
    45
    Rep Power
    0

    Smile

    Looks like you nailed it pbrockway2 :) I didn't realize I was typing .java after the command line, and now it all cleared up, thanks for figuring out the actual reason for that problem! :)

    -Derek Raimann

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. changing current directory
    By flaca in forum New To Java
    Replies: 7
    Last Post: 03-06-2011, 03:50 AM
  2. Static function to get current working directory
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 06-23-2008, 03:13 AM
  3. How to get Current Directory through File
    By Java Tip in forum java.io
    Replies: 0
    Last Post: 04-05-2008, 11:14 AM
  4. How to get the current working directory in EJB?
    By sathish_2111 in forum Enterprise JavaBeans (EJB)
    Replies: 1
    Last Post: 07-19-2007, 05:24 PM
  5. How can i get current directory?
    By Ashley in forum New To Java
    Replies: 1
    Last Post: 05-26-2007, 02:21 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •