jitron
jitron

Reputation: 135

When should I set class path?

public class a {
public static void main(String[] args) {
    System.out.println("Hello, World");
    }
}

For the above code, I can run it by javac a.java, and then java a.

But if I add a package for it:

package hello;
public class a {
public static void main(String[] args) {
    System.out.println("Hello, World");
    }
}

I need add the classpath -cp in order to run it: java -cp ../ hello.a

Why I do not need to set the classpath in the first situation? When do I need to add -cp?

Upvotes: 2

Views: 407

Answers (3)

Marquis Blount
Marquis Blount

Reputation: 8095

To answer your question

When should I set my classpath

Always, as you work on more complex projects you will find that your classpath will almost always need to be set. This will either be done manually like you have done with -cp command or by your IDE.

To answer your second question

Why I do not need to set the classpath in the first situation

I first need to explain a little bit about classpaths. In short classpath exist to tell the VM where to look for your files. In the first situation since you didnt have a package the default location was used to find your class so no classpath was needed. However when you complicated things and added a package at this point a classpath is needed

Upvotes: 1

WaliedYassen
WaliedYassen

Reputation: 65

Classpath is like telling the system where to find my classes:
If you don't have a classpath the java will try to load the class
from the default directory (Probably where you're running the command at).

Now let's say you put your compiled classes in folder "bin" and sources in "src" folder
To tell the system to load the classes from "bin" folder you have to give him the following parameter: -cp bin;


Also i see you don't understand the packaging system in java so here's fast explain:
Packaging is like you the directory of your class for example:
If you set the class's package to package a; and your classpath directory is set to "bin"
You have to create folder called "a" in "bin" folder, and then move the compiled class there, do the same for the source file, but in "src" folder

Just saying you could use eclipse which is located at : http://www.eclipse.org

If this didn't help you, Then take a look at this: https://www3.ntu.edu.sg/home/ehchua/programming/java/J9c_PackageClasspath.html

Upvotes: 1

yshavit
yshavit

Reputation: 43391

The classpath is where java (the program) looks for classes. The default contains a bunch of system-wide things (for the JDK), and then also the current directory: ..

Without the package line, your class was in the "default package," which is basically no package. This means its full name is a (more or less), and java will look for it in a file called $CP_ELEM/a.class for each element CP_ELEM in the classpath. In the default case, that amounts to ./a.class, which is fine because that file exists.

With the package line, your class is in the hello package, and its full name is hello.a. That means that java will look for it in $CP_ELEM/hello/a.class, which amounts to ./hello/a.class -- which doesn't exist. But if the directory you're in happens to be called "hello", then java -cp .. hello.a, which amounts to looking in ../hello/a.class, will work.

Upvotes: 1

Related Questions