0

I plan on becoming a certified Java programmer and am studying from the Sierra-Bates book. I had a question about classpaths. Do classpaths need to find only the supporting classes of the class I'm running/compiling, or the supporting classes and the class itself? Also, when I'm getting classes in packages from classpaths, is it legal to just put the adress of the file(the path to it), instead of putting it's root package. Thanks.

2 Answers 2

1

1 - a classpath has to give access to each class that needs to run in your program. That would include the main class and any classes it calls and those they call. If there is some code in one of those classes that is never called, in many cases, you don't need to have the classes referenced by the uncalled code.

2 - you have to put the root of the packages in the classpath. So a class "com.bob.myprog.Main" would need to have the class path point to the folder where the "com" package/folder lies. It will need to contain a "bob" folder and "bob" will need to contain a "myprog" folder with "Main.class" in it.

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

4 Comments

What about compiling? Consider the following directory structure: food->test->xcom->A.class, B.java (where"->" meaning the directory on the left containst the directory/file to the right) and the following code: package xcom; public class A { } package xcom; public class B extends A { } With test as the current directory, how would you compile B.java with javac from the command line?
The folder containing "xcom" will need to be on the classpath. Thats "test".
That was a bad example, sorry. What I wanted to ask was does the classpath need to find only A.class, or A.class AND B.java.
It only needs to find the classes you use. If your main is in A, you need A. If, then, A calls or otherwise uses B, you need B, too. If main is in A and A does not use B, B isn't needed. But the class path goes to a folder only and that folder, in this case, is 'test'. The package in classes A and B will provide the rest of the path.
0

Classpath has to contain both the supporting classes and the class itself.

However, sometimes you can run a single file without specifying classpath (and it will work). As specified in http://docs.oracle.com/javase/tutorial/essential/environment/paths.html :

The default value of the class path is ".", meaning that only the current directory is searched. Specifying either the CLASSPATH variable or the -cp command line switch overrides this value.

Therefore, if you have a class MyClass compiled in the current directory, the following will work:

java MyClass

while pointing classpath to another directory will lead to an error (classpath no longer contains MyClass):

java -cp lib MyClass



When you have a class in a package, it is not enough to put the address to the class file in the classpath. According to SCJP Sun Certified Programmer for Java 5 Study Guide:

In order to find a class in a package, you have to have a directory in your classpath that has the package's leftmost entry (the package's "root") as a subdirectory.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.