Reputation: 251
Directory path:
c:\home\test\src\com\bsoft\conc
I have my java program in src
folder and I have my class file in conc
folder.
I need to run my java program from home
folder.When I run I'm getting error:
could not find or load main class
Upvotes: 12
Views: 25959
Reputation: 444
In Linux ( Simple scenario with out consider them as packages )
> parent
|
|- stdlib
| |
| -Library.java
|- Main.java
COMPILE FROM PARENT > javac -cp ./stdlib: Main.java
RUN FROM PARENT > java -cp ./stdlib: Main
Upvotes: 0
Reputation: 5106
The answers here don't touch on the spaces problem.
If your path has spaces, you must wrap it inside the quotes, otherwise an error would show up:
C:
Program Files
MyProject
src
testpackage
Test.java
target
classes
testpackage
Test.class
package testpackage;
public class Test {
public static void main(String[] args) {
System.out.println("Test");
}
}
java -cp "C:\Program Files\MyProject\target\classes" testpackage.Test
Upvotes: 1
Reputation: 21
I had a similar problem where I was trying to run a Java program that calls a method in a class that is in another directory. I read this page and added the directory to my classpath, but I made the mistake of using '~' which in Bash means '/home/user/'.
So this command did NOT work
java -classpath ~/CurrentDirectory:OtherDirectory program
But this command did
java -classpath /home/user/CurrentDirectory:OtherDirectory program
Upvotes: 2
Reputation: 251
In my program com.bsoft.conc is a package name where my class file for the compiled program will be stored.If I have to run that from home folder we have to specify java -classpath test\src com.bsoft.conc."class-file-name"
This is because we need to tell the JVM where it has to look for class file.
so , we have to specify navigation to the src using "test\src" and then class file location "com.bsoft.conc.class-file-name"
If you have set environment variable in advanced settings then it will also be overriden if you specify classpath in cmd
Upvotes: 4
Reputation: 358
Set your class path for this java file:
java -cp C:\hello\build\classes com.javahowto.test.HelloWorld
or using Environment variables and run it from any third location from that machine.
Upvotes: 11
Reputation: 13709
It's time for you to read on about classpath
( a way to tell java compiler where to look for the class file you intend to run ).
Basically there are two ways to set classpath
-classpath
or -cp
command line arg to javac
commandRefer and read the below links completely
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/classpath.html
Upvotes: 6