2

I know this has come up a number of times, but previous responses just don't seem to help. My environment variables are :

  • CLASSPATH C:\Program Files\Java\jre7\lib;C:\Program Files\Java\jdk1.7.0_15\bin;
  • PATH C:\Program Files\Java\jdk1.7.0_15\bin;

When moving to the directory as follows C:\Users\Oli\My Documents\java I can compile using javac, but cannot runt he program using java. I know its most likely got something to do with environment variables but I cannot get it to work. P.S the error is "could not find or load main class"

Any help would be appreciated.

4 Answers 4

3

CLASSPATH is the place where JRE looks for classes. You've set your CLASSPATH to a value and expect to run the class from current Directory, which won't work.. for instant solution you may use

java -cp C:\Users\Oli\My Documents\java ClassName

Or undo setting CLASSPATH. Default CLASSPATH is current directory

Sign up to request clarification or add additional context in comments.

Comments

3

Lets assume that your ".java" file default package ( no package defined) survivies in "C:\Src" You dont need to set the CLASSPATH in this case.

  cd C:\Src
  javac MyJava.java
  java MyJava

If with package say com.test

cd C:\Src
javac com\test\MyJava.java
java com.test.MyJava

However if you are not in the same folder as Source files and want to run from anywhere

set CLASSPATH=%CLASSPATH%;C:\src
javac MyJava.java or javac com\test\MyJava.java

and java com.test.MyJava or java com.test.MyJava

Comments

2

Unset CLASSPATH and just use the default one provided by the JVM. Here is a link to the Java Tutorial that covers the environment variables.

Comments

0

Seems like the problem is not in the path... Does your code use the 'package' statement? (i.e. package my_package;)

If so, go to 'java' directory and execute:

java my_package.MyClass

where 'my_package' is the name of... the package, and MyClass is your compiled .java file (without the .class extension).

Good luck.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.