Results 1 to 5 of 5
  1. #1
    kulangotski is offline Senior Member
    Join Date
    Feb 2009
    Posts
    117
    Rep Power
    0

    Default cannot find the class file when running the java command

    hello, thanks for reading.

    I am an eclipse user and running files(classes) from the terminal was a bit forgotten to me.
    tried to play with my terminal but it gets me to a situation where I had no clue.

    I can do the java command 'javac Hello.java'. it reads for a while and then suddenly you have a .class file in the place where the .java file exists but when I do the the javac like 'java Hello', it would say

    Java Code:
    Exception in thread "main" java.lang.NoClassDefFoundError: Hello
    I believe I have the classpaths set because I can invoke the 'javac' command. and my classpath is
    JAVA_HOME=/usr/lib/jvm/java-6-openjdk:/usr/lib/jvm/java-6-openjdk/bin
    CLASSPATH=$TOMCAT_HOME/lib:$TOMCAT_HOME:$JAVA_HOME

    I even copied the Hello.class file to the same directory where the java command lies. in the bin directory and it would still give me that friendly message

    so I am bit clueless right now, do I have something wrong with my paths? or maybe with the files I am creating?

    Any suggestions would be appreciated :D

    regards,
    simon

  2. #2
    kulangotski is offline Senior Member
    Join Date
    Feb 2009
    Posts
    117
    Rep Power
    0

    Default

    update:


    tried to do the command "java -classpath . Hello" and it worked. I was wondering why it was no able to act upon the classpath that I set on my system variable?

  3. #3
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Quote Originally Posted by kulangotski View Post
    I was wondering why it was no able to act upon the classpath that I set on my system variable?
    The environment variable settings are normally used by the command processor to search for the executables and batch files, like *.exe and *.bat files. So when you type "java", it actually goes to your java installations bin directory, where it finds the java.exe and it executes the further things.

    If you have placed your generated .class file inside the bin directory [which is in environment variable], then you would have to explicitly mention the class path for that file using the -cp or -classpath option. In your case, on a windows machine you would have to run,

    Java Code:
    java -cp "%path%" Hello
    which would run your program correctly.

    One more thing, whenever you specify the -cp or -classpath option along with java, it overrides the classpath environment variable settings declared.

    Hope that helps,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  4. #4
    kulangotski is offline Senior Member
    Join Date
    Feb 2009
    Posts
    117
    Rep Power
    0

    Default

    Hi,

    That was too much help, now I understand. Thanks for replying :D

    regards,
    simon

  5. #5
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    You are Welcome :D
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

Similar Threads

  1. error in the Command Prompt when running a bat file
    By bluesagemedia in forum New To Java
    Replies: 1
    Last Post: 10-03-2010, 01:05 AM
  2. Replies: 3
    Last Post: 08-25-2010, 10:21 AM
  3. running command prompt using java
    By xclaim in forum New To Java
    Replies: 1
    Last Post: 01-14-2010, 03:46 AM
  4. Replies: 1
    Last Post: 06-30-2009, 02:32 PM
  5. Replies: 0
    Last Post: 05-13-2008, 06:16 AM

Posting Permissions

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