• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question about packages.

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

What Christophe wrote doesn't look correct.


Sorry, that isn't correct indeed.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic