I've been using Eclipse to practice programming Java for a while. Haven't got any problems so far, but today I just wanted to compile and run my code on the command line (I'm using Windows OS) and it's not working.
Suppose the compiled file name is 'Test.class'. After compiling, when I put 'java Test' on the command line then error message says, "couldn't find or load main class Test"
But I have set all the system variables like %JAVA_HOME%, Path, CLASSPATH and had no problems running that code on Eclipse.
Below are screenshots of my setting information on the command line. (Please ignore the Korean words)
when I entered 'java' on the command line
when I entered 'javac' on the command line
No Problems, Right? But the error occurred, when I entered 'java Test' to run this code after compiling Test.java to Test.class (The letters you can't understand is Korean, ignore it, it says "couldn't find or load main class Test")
Help me Please!
------------------Below is additional screenshot----------------------- ... Can you see? I compiled TestClass.java and converted it into TestClass.class but when I executed "java TestClass", the error appears.
Below is my code..... I did almost nothing on the code, just wanted to test command line method....
TestClass.java
package test;
public class TestClass {
public static void main(String[] args) {
System.out.println("SOS");
}
}
The problem is your CLASSPATH
environment variable. It exists, but doesn't include the current directory.
Unless you really need the CLASSPATH
environment variable for some reason, I would remove it - then it will default to just the current directory. All the framework classes are already on the boot classpath - you don't need to worry about those.
If for some reason you can't change your environment variables, just specify the classpath on the command line:
java -cp . TestClass
See more on this question at Stackoverflow