aspose file tools*
The moose likes Beginning Java and the fly likes Question about packages. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question about packages." Watch "Question about packages." New topic
Author

Question about packages.

Andrew Stallard
Ranch Hand

Joined: Mar 06, 2010
Posts: 33
Whenever I try to use package declarations on classes, even when I put them in a folder with the same name as the package declaration, are unrecognizable to other classes. However, when using the NetBeans IDE I do not have this problem.

For Example:




Here is the class CircleTest in the same folder called "graphics:"


When attempting to compile, I get these errors, indicating the class Circle can not be found:

CircleTest.java:4: cannot find symbol
symbol : class Circle
location: class graphics.CircleTest
Circle quack=new Circle();
^
CircleTest.java:4: cannot find symbol
symbol : class Circle
location: class graphics.CircleTest
Circle quack=new Circle();
^
2 errors

Nevertheless, it runs just fine in the IDE.


Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

You need to put the graphics package in the classpath. On the command line, this can be done using "javac -cp . CircleTest.java" in the graphics directory. IDEs are smart enough to find other classes in the same package, not the command line.


[My Blog]
All roads lead to JavaRanch
Andrew Stallard
Ranch Hand

Joined: Mar 06, 2010
Posts: 33
Christophe Verré wrote:You need to put the graphics package in the classpath. On the command line, this can be done using "javac -cp . CircleTest.java" in the graphics directory. IDEs are smart enough to find other classes in the same package, not the command line.




Unfortunately, that didn't work.

Thanks anyway
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14269
    
  21

What Christophe wrote doesn't look correct. You have to put the graphics package in the classpath, as he says. But you don't do that by going into the directory 'graphics' and then specifying '-cp .'.

Instead, go to the parent directory that of the 'graphics' directory, and add that to the classpath. For example:

C:\MyProject\graphics> cd ..

C:\MyProject> javac -cp . graphics\CircleTest.java

C:\MyProject> java -cp . graphics.CircleTest


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

What Christophe wrote doesn't look correct.

Sorry, that isn't correct indeed.
Rahul Kurup
Greenhorn

Joined: Dec 11, 2009
Posts: 9
This is a compilation error rather than a runtime NoClassDefFound error. So I don't think setting CLASSPATH will help.

While compiling from command line remain one level higher from the package structure. Lets say your CircleTest.java file is in D:\java\graphics then navigate to D:\java and execute

javac graphics\*.java

This will compile and place the class files in your graphics folder. If you want the class files in some other folder you can use -d option of javac.



Rahul J.
Andrew Stallard
Ranch Hand

Joined: Mar 06, 2010
Posts: 33
Jesper Young wrote:What Christophe wrote doesn't look correct. You have to put the graphics package in the classpath, as he says. But you don't do that by going into the directory 'graphics' and then specifying '-cp .'.

Instead, go to the parent directory that of the 'graphics' directory, and add that to the classpath. For example:

C:\MyProject\graphics> cd ..

C:\MyProject> javac -cp . graphics\CircleTest.java

C:\MyProject> java -cp . graphics.CircleTest





Yes, that worked. Thank You.

However, you need to use the frontslash, at least if you've been liberated from microserfdom.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question about packages.