3

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? alt text

1
  • 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 Commented Nov 11, 2010 at 4:59

5 Answers 5

2

The current directory should be in the default CLASSPATH, but maybe it's not. Try java -cp . Assignment

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

Comments

0

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?

Comments

0

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....

Comments

0

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

Comments

0

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

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.