Reputation: 13659
I'm learning java and I would like to know how to compile a java program from other directory.
For example, my compiler is in my drive c:\
and I want to compile my java program from drive e:\
How should I do that?
I'm getting this error, what does it mean?
Upvotes: 3
Views: 7149
Reputation: 8620
Is there a package
declaration at the top of your Assignment.java? If so, remove it and recompile for a quick fix.
To work with Java packages, you'll need a directory structure that matches the package declarations.
For example, say this is your Assignment.java:
package myjava;
public class Assignment {
public static void main(String[] args) {
....
....
}
You run this command to compile:
E:\java>javac -d . Assignment.java
And you get myjava\Assignment.class if all went well. The -d .
option means "place generated class files in the current directory". javac
creates the package hierarchy as directories for you.
Now that your directories match your packages, this should work:
E:\java>java myjava.Assignment
Upvotes: 0
Reputation: 3689
You are using package in your code...so it shows NoClassDefFoundError when you run you should create folder which contain your package name...compile that java file...and you can run that file from previous directory of that java file directory...
For example your code is
package test;
class Assignment{
public static void main(String args[]){
System.out.println("Hai");
}
}
it saved on this path "E:\java\test
"
compile this file and you can run this file from this path "E:\java
"
command to run this file
java test.Assignment
E:\java> java test.Assignment
Upvotes: 0
Reputation: 7
Well the easy way is to set ur classpath variable. Since from screen shot it seems ur using windows i suggest u right click the my computer nd select properties. Go to advance setting and click environment variable tab.
Then a new window pops up which has System Variable at bottom. Select new and create a variable JAVA_HOME = path where u installed java eg for me its c:\java
Now once u add this search an existing variable path and choose edit. Now at the end append the following ;%JAVA_HOME%\bin Now ur done u cn run java program frm any location on ya comp....
Upvotes: 0
Reputation: 3413
It's been a while since I've done java, but it seems like the compiling isn't your problem. Since javac
returns properly, it seems to be a problem with Assignment.java
. Does your Assignment
class have a main method?
Upvotes: 0
Reputation: 3508
The current directory should be in the default CLASSPATH, but maybe it's not. Try java -cp . Assignment
Upvotes: 2