Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about packages.

 
Andrew Stallard
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Andrew Stallard
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15436
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What Christophe wrote doesn't look correct.

Sorry, that isn't correct indeed.
 
Rahul Kurup
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Andrew Stallard
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic