File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Problem in javac and java command Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem in javac and java command" Watch "Problem in javac and java command" New topic
Author

Problem in javac and java command

Nimi Verma
Greenhorn

Joined: Jul 02, 2011
Posts: 2
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

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Aug 16, 2005
Posts: 14430
    
  23

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Ogeh Ikem
Ranch Hand

Joined: May 13, 2002
Posts: 180
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

Joined: Oct 13, 2005
Posts: 40052
    
  28
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

    Joined: Jul 16, 2011
    Posts: 90
    It should be:
    public static void main(String[] s)

    and not:
    public static void main(String s[])
    Seetharaman Venkatasamy
    Ranch Hand

    Joined: Jan 28, 2008
    Posts: 5575

    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

    Joined: Jul 16, 2011
    Posts: 90
    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

    Joined: Jul 16, 2011
    Posts: 5


    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

    Joined: Jul 16, 2011
    Posts: 90
    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

    Joined: Oct 15, 2010
    Posts: 294

    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.

    SQL Query Interview Question
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Problem in javac and java command