• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem in javac and java command

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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


 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
     
    Ranch Hand
    Posts: 90
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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 Windows XP Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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 ?
     
    Greenhorn
    Posts: 5
    Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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)

     
    Ranch Hand
    Posts: 300
    Eclipse IDE Firefox Browser Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic