tags:

views:

97

answers:

5

I am trying to run a java program from command line. I tried following the steps mentioned here. But when I try to run javac Hello.java, it's throwing error that such a program is not there. I tried giving java Hello.java and got the error:

Exception in thread "main" java.lang.NoClassDefFoundError: Hello/java
Caused by: java.lang.ClassNotFoundException: Hello.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: Hello.java.  Program will exit.

What is the problem here. How can I do it?

EDIT: I have many classes in my code file, Hello.java. Will that cause any problem?

+5  A: 

To run the program you need to do:

java Hello

which is java followed by the class name without extension.

codaddict
Still I am getting the "ClassNotFoundException" that I pasted above. My code is running fine in eclipse.
iJeeves
Or, depending on your environment, "java -classpath . Hello"
Matthew Wilson
Can you edit your question to make clear that when running you program with `java Hello` it still doesnt work ? And can you put the stacktrace you get when running `java Hello` ?
Guillaume
I didn't have JDK. I thought eclipse will install everything. Now it works
iJeeves
+5  A: 

First you should compile the java code with

 javac Hello.java

Then run it

 java Hello

In both cases, make sure your classpath is set correctly...

Matthieu
Where can I find javac. It is not there in jre/../bin
iJeeves
javac is in the JDK
Carlos Tasada
http://www.oracle.com/technetwork/java/javase/downloads/index.html - the JRE is to be able to "run", the JDK to be able to compile.
Matthieu
+1  A: 

As stated by the others answer, first, you have to run your application using java Hello and not java Hello.java

Second, you have to check that your CLASSPATH is correctly set. It seems that your variable is not set or does not integrate the current directory, i.e. .

So run :

javac -classpath . Hello.java
java -classpath Hello

or

set CLASSPATH=.
javac Hello.java
java Hello

Of course, defining the CLASSPATH as a user / system variable in your Windows system is a better solution!

romaintaz
A: 

In case Hello.java is contained in a package, you will have to create an appropriate directory structure. I.e. in case Hello.java is contained in the package com.stackoverflow, you must create the folders com/stackoverflow and put Hello.java in this folder. From the root folder you must then launch

java com.stackoverflow.Hello

Yaba
+2  A: 

First off, java requires at most one public class per file. No

public class this {
}

public class that {
}

You can have

class this {
   class that {
   }
}

if you need.

EDIT or in file this.java:

public class this {
}

class that {
}

javac won't be in the jre folder. Have you installed the jdk? it doesn't come by default on many computers. it's often in "C:\Program Files\Java\jdk1.6.0_05\bin\javac.exe" or a similiar path.

Michael
After installing jdk, I am able to run javac Hello.java and then java Hello. It runs correctly although I am having multiple (non-nested) classes.
iJeeves
That's correct - editing post. only one public class, which must have the same name as file.
Michael
Also, eclipse's javac is in a different place, and in IBM RAD (based on eclipse), it's not in the PATH environment variable. I don't know precisely where it would be. Sorry for the confusion.
Michael