Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / April 2008

Tip: Looking for answers? Try searching our database.

How to include *.class file with package in CLASSPATH?

Thread view: 
Ulf Meinhardt - 26 Apr 2008 16:17 GMT
Assume I have an already compiled java class in the following location:

D:\java\output\aaa\bbb\ccc\myclass.class

myclass.java contains the following package information:

package aaa.bbb.ccc;

The current path in the command prompt is

D:\java>

I want to compile another java class test.java which uses the class myclass e.g. in a statement like

myclass dummy = new myclass();

But when I enter a statement like:

javac -cp D:\java\output\aaa\bbb\ccc;%CLASSPATH% test.java

then the compiler complains that he cannot find myclass:

test.java:22: cannot access myclass
bad class file: D:\java\output\aaa\bbb\ccc\myclass.class
class file contains wrong class: aaa.bbb.ccc.myclass
Please remove or make sure it appears in the correct subdirectory of the classpath.
         myclass dummy = new myclass();
         ^
1 error

Why?

I added D:\java\output\aaa\bbb\ccc to the CLASSPATH. Shouldn't that be sufficient?

Ulf
Lion-O - 26 Apr 2008 16:26 GMT
> D:\java\output\aaa\bbb\ccc\myclass.class
>
[quoted text clipped - 5 lines]
>
> D:\java>

---<CUT

> But when I enter a statement like:
>
> javac -cp D:\java\output\aaa\bbb\ccc;%CLASSPATH% test.java
>
> then the compiler complains that he cannot find myclass:

Try thinking of packages being directories when you're working on the
commandline. You already seem to be doing that since you've setup a directory
structure aaa\bbb\ccc. Now, in order to find the classes in the package the
compiler expects to be able and access a directory structure aaa\bbb\ccc which
then contains myclass.class.

But your classpath doesn't have that, it only points to the directory where
myclass.class resides but that directory doesn't contain the actual directory
structure.

Try just compiling without setting any classpath, or, if your classpath
contains specific settings try: javac -cp .;%CLASSPATH test.java

Groetjes, Peter

Signature

.\\ PGP/GPG key: http://www.catslair.org/pubkey.asc

Lew - 27 Apr 2008 03:03 GMT
>> D:\java\output\aaa\bbb\ccc\myclass.class
>>
[quoted text clipped - 13 lines]
>>
>> then the compiler complains that he cannot find myclass:

> Try just compiling without setting any classpath, or, if your classpath
> contains specific settings try: javac -cp .;%CLASSPATH test.java

Arne gave the correct answer, which this wasn't.  He also suggested to follow
the coding conventions, which have us use upper-case letters to begin class
names (and lower-case ones to begin variable and method names), and camel case
thereafter.

Additionally, having the word "class" in a class name is silly.

CLASSPATH is a blunt tool.  Normally one shouldn't use it, at least not much.
 Use the -cp option completely.

To run Test (correcting the class name), and assuming a dependency on
MyExample in package aaa.bbb.ccc (directory d:\java\output\aaa\bbb\ccc\) and
Test in package xxx.yyy.zzz, and the current working directory d:\work\,

java -cp D:\java\output xxx.yyy.zzz.Test

Of course, Test.java will start off with these directives:

package xxx.yyy.zzz;
import aaa.bbb.ccc.MyExample;

Signature

Lew

Arne Vajhøj - 26 Apr 2008 16:33 GMT
> Assume I have an already compiled java class in the following location:
>
[quoted text clipped - 17 lines]
>
> then the compiler complains that he cannot find myclass:

javac -cp D:\java\output;%CLASSPATH% test.java

Arne

PS: Class names are by convention capitalized.
Roedy Green - 27 Apr 2008 11:44 GMT
>Assume I have an already compiled java class in the following location:

see http://mindprod.com/jgloss/classpath.html
http://mindprod.com/jgloss/package.html
http://mindprod.com/jgloss/import.html
http://mindprod.com/jgloss/javacexe.html
http://mindprod.com/jgloss/javaexe.html
Signature


Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Joshua Cranmer - 27 Apr 2008 23:16 GMT
> I added D:\java\output\aaa\bbb\ccc to the CLASSPATH. Shouldn't that be sufficient?

It should be D:\java\output\.

Java assumes that a class `java.lang.Object' would be stored in
java\lang\Object.class with some path from the classpath prepended to
it, be it jar:rt.jar! (e.g., for a jar file) or D:\java\output\.

Signature

Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth

CHAFIK Wassime - 28 Apr 2008 16:12 GMT
> Assume I have an already compiled java class in the following location:
>
[quoted text clipped - 31 lines]
>
> Ulf

sure you didn't forgot
import aaa.bbb.ccc.myclass; //you should  really name it  MyClass
in the test.java
and then no need to add something to the classpath if test.class and the
"aaa" folder are both in the same folder "output" in your case maybe
the classloader knows that his looking for "myclass.class" under
aaa/bbbb/ccc/  :-)
hope that helps
evan - 30 Apr 2008 00:08 GMT
> Assume I have an already compiled java class in the following location:
>
[quoted text clipped - 31 lines]
>
> Ulf

Try putting D:\java\output to your CLASSPATH instead of D:\java\output
\aaa\bbb\ccc


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.