Reputation: 29248
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?
Upvotes: 1
Views: 390
Reputation: 3345
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.
Upvotes: 3
Reputation: 5979
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
Upvotes: 1
Reputation: 81667
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!
Upvotes: 1
Reputation: 16417
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...
Upvotes: 6
Reputation: 455440
To run the program you need to do:
java Hello
which is java
followed by the class name without extension.
Upvotes: 5