Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem in javac and java command

 
Nimi Verma
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,
I have installed java jdk on my pc. Till yesterday it was running fine but today it is giving a strange problem. Actually the problem is when I'm typing javac command in command prompt (win xp) it shows no error and when I run my program with java command it says Exception in thread "main" java.lang.NoClassDefinitionFoundError: Hello
I know this error is shown if main is not in the class definition but I do have main in the class. My program is very simple one


The strange thing about the problem is when I'm changing main to something like mainn it doesn't show any error even then, but if I put a semi-colon (;) after public static void main(String s[]) like this public static void main(String s[]); it shows error. What I think is somehow main is not getting compiled with javac command. Or could it be problem of installation. But till yesterday it was running fine. I have jdk 1.4 installed on my pc. So please tell me about the problem and thanks a lot.

Well, after much fuss the problem has got solved but I have permanently set the class path in Environment Variables as .;
But if tomorrow I want to run servlets I'll have to set classpath for that also. So at that time will I remove this classpath or many classpaths can be set simultaneously?
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java Zealot wrote:
I know this error is shown if main is not in the class definition but I do have main in the class. My program is very simple one

really?I think It will give you java.lang.NoSuchMethodError; coming to your problem, probably this is a classpath issue, check that!
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15284
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error means that Java cannot find the class that you are trying to run.

First, check if you have a file named Hello.class in the current directory. If you have that, try to run it with a command like this:

java -cp . Hello

The -cp option is to specify the classpath, and the . is the current directory. The classpath is the set of directories that Java searches for class files. So, with -cp . you tell Java to look in the current directory for the Hello.class file.
 
Ogeh Ikem
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you run the java command, the JVM will throw java.lang.NoClassDefFoundError if it cannot find the class. You must set the classpath to the location of the class before running the java command, as follows:



When you run the java command, the JVM will throw java.lang.NoSuchMethodError if it cannot find a method with the following signature


 
Campbell Ritchie
Sheriff
Pie
Posts: 48972
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What was the system classpath before you set it? It usually does more harm than good to set a system classpath or a user classpath, so I recommend:
  • If there was nothing in the classpath before you set it, delete your system and user classpath.
  • If there was something in the classpath before you set it, add .; to its beginning.
  • Always use the -cp option Jesper de Jong showed, or the set command, as Ogeh Ikem showed you, to set your classpath, because it will be different for each application.
     
    Angus Comber
    Ranch Hand
    Posts: 90
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It should be:
    public static void main(String[] s)

    and not:
    public static void main(String s[])
     
    Seetharaman Venkatasamy
    Ranch Hand
    Posts: 5575
    Eclipse IDE Java Windows XP
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Angus Comber wrote:It should be:
    public static void main(String[] s)

    and not:
    public static void main(String s[])

    Ok, I prefer 1st one. but there is no problem with 2nd idiom.
     
    Angus Comber
    Ranch Hand
    Posts: 90
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ah, OK, I wondered if it was problem. Didn't realise other variant was possible.

    Then op should state the command line used.

    Is file definitely called Hello.java and are you typing javac Hello.java ?
     
    Manoj Kumar M
    Greenhorn
    Posts: 5
    Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    use this command to set the classpath for the current folder along with the original classpath

    set classpath=%classpath%;.

    here you no need to remove the existing classpath

    while running the code use verbose command. this will display the classpath. you can check whether your classpath got loaded.
    java -verbose <classname>
     
    Angus Comber
    Ranch Hand
    Posts: 90
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The user should not need to set the classpath to run this simple Java code. The path yes, but not the classpath. The code does not refer to any user defined classes or packages.

    See info on classpath here:
    http://en.wikipedia.org/wiki/Classpath_(Java)

     
    Javin Paul
    Ranch Hand
    Posts: 295
    Eclipse IDE Firefox Browser Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Nimi Verma wrote:Hi everyone,
    I have installed java jdk on my pc. Till yesterday it was running fine but today it is giving a strange problem. Actually the problem is when I'm typing javac command in command prompt (win xp) it shows no error and when I run my program with java command it says Exception in thread "main" java.lang.NoClassDefinitionFoundError: Hello
    I know this error is shown if main is not in the class definition but I do have main in the class. My program is very simple one


    The strange thing about the problem is when I'm changing main to something like mainn it doesn't show any error even then, but if I put a semi-colon (;) after public static void main(String s[]) like this public static void main(String s[]); it shows error. What I think is somehow main is not getting compiled with javac command. Or could it be problem of installation. But till yesterday it was running fine. I have jdk 1.4 installed on my pc. So please tell me about the problem and thanks a lot.

    Well, after much fuss the problem has got solved but I have permanently set the class path in Environment Variables as .;
    But if tomorrow I want to run servlets I'll have to set classpath for that also. So at that time will I remove this classpath or many classpaths can be set simultaneously?



    Your problem is clearly with classpath.

    1) setting environment variable CLASSPATH as "." is not a good option, though you should always include "." in it.

    2) reason of NoClassDefFoundError is not with main method but it occur when your compiled .class file is not present in classpath when you run "java" command and that class needs to be loaded by Classloader.loadClass() or Classloader.findSystemClass().

    3) In Java if you provide -cp option than environment variable CLASSPATH is not used same is the case if manifest file of jar contains classpath , that overrides both CLASSPATH and -cp option . to learn more about How Classpath works in Java see my post.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic