views:

82

answers:

6

I have two classes:

MyApplication
Library

The Library has already been compiled into Library.class and the source code is no longer available. I am now trying to compile MyApplication from source. MyApplication depends on the Library. The Library has a package name of org.myCompany. I tried setting my classpath to the following:

set CLASSPATH=C:\java\project\org\myCompany\Library.class;.

which produced the following javac compiler error message:

MyApplication.java:33: cannot find symbol
symbol: class Library
location: class MyApplication
          Library theLibrary = new Library();

So I changed my classpath to be:

set CLASSPATH=C:\java\project\;.

which produced the exact same error message.

How do I set my Windows classpath to include the Library.class file? Should it point at the folder contains the org\myCompany subfolders? Or point directly to the class file? Or to the folder containing the class file (even though the class is in a package and belongs in a subfolder)?

I do an echo %CLASSPATH% after my set command and the classpath is being set correctly. I also made an ant build.xml file and encountered the same problem. In fact, ant -verbose confirmed that my classpath is being set correctly.

A: 

You cannot add a single class in your classpath like this. You have 3 solutions:

  • add this class in the path of your other compiled classes (respecting the package naming of your directories)
  • add the root directory of this class in your classpath (in your case "C:\java\project\")
  • add this single class into a jar and add this jar to the classpath

For your problem, the thrird choice is cleaner: external dependencies normally are packaged into jar files.

Benoit Courtine
A: 

If your .class file isn't in jar file, point your classpath to the parent dir where package of class resides, e.g., for class org.myCompany.Library, point your CP to directory containing org/myCompany.
If your .class file included into some jar file, add full path to that jar to your classpath.

Victor Sorokin
actually I don't have any JARs for either of the two classes
David
@David: You can add .class file to jar. Main advantage that if you have lot of classes packed into jar, you will need add only that jar file to classpath.
Victor Sorokin
A: 

If you compiled the class files to a different directory, the classpath needs to point to where the .class file is.

set CLASSPATH=C:\java\project\;

is correct assuming that that the class file is in the same directory as the .java source file.

Kelly French
A: 

Is there a problem locating the Library to the same root project where is your MyApplication class

Example, if:

c:/project/org/company/MyApplication.class

Can you locate the Library class into:

C:/project/org/myCompany/Library.class

please notice, that the folders org/myCompany and org/company are located under the same folder c:/project/.

Please notices that this solution works for you if the Library Class is only used by your application.

Edited Windows command prompt is tedious, after setting the classpath please close and re-open the Command Prompt, so it can see the new classpath's value.

Garis Suero
+2  A: 

First of all: the use of the CLASSPATH environment variable is very strongly discouraged. The best thing is for you to forget that it exists. Use the -cp command line switch or similar methods to set the classpath.

Second, the classpath entries each represent a place where the classloader will start looking for .class according to the package hierarchy, i.e. it will look for the class org.myCompany.Library in a subfolder org/myCompany in any of the classpath entries.

Therefore, if

  • you add a classpath entry C:\java\project\
  • and there is a class file C:\java\project\org\myCompany\Library.class
  • which is actually part of a package org.myCompany (capitalization matters here!)
  • and your MyApplication class has an import org.myCompany.Library;

Then it really should work.

Michael Borgwardt
A: 

For the classpath to work, you need to have a folder structure which matches the package hierarchy. So if your class is org.myCompany.Library, you must create a nested folder structure of C:\java\project\org\myCompany and place your Library class file in the myCompany folder. Then set the class path to C:\java\project\

Stormshadow