• 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

basic instantiation question

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I'm just learning Java, and can't figure out why I'm getting an error on this. I've read several tutorials, and it seems like everything is OK, but obviously it must not be. The file2.java file won't compile, generating a "Cannot Find Symbol" error. Does anyone see what I'm doing wrong here? Thanks!

file1.java

file2.java

I've created these two files on my desktop, and added /home/brad/Desktop to my $CLASSPATH. (I'm using Linux). I compile file1.java first, which works fine. Then I try to compile file2.java, which doesn't compile.
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you use a package named testpkg, your files need to be located in a folder called testpkg.
 
Brad Clouser
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks!!! That worked perfectly.
Do you know if there is any alternative to modifying the classpath when working with multiple classes? Is there any other way to tell the java compiler where to look? My goal with this question is to avoid the need for someone else to modify their CLASSPATH if I send them my java files. Thanks for your help!
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Generally you don't want to use the CLASSPATH environment variable at all. Both the Java compiler and the runtime accept a -classpath command-line switch; rather than setting CLASSPATH, you can and should always use the -classpath argument instead:

java -classpath c:\myclasses file2


 
Brad Clouser
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks! I will use the -classpath switch in the future. Thanks for your help.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are just beginning Java, it might be easier to miss out package names until you are more experienced.
 
Brad Clouser
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the tip. I took out the package names. I only had them in there because a lot of the tutorials I read started with "Assuming the files are in the same package..." Everything is working perfectly now. Thanks everyone!
reply
    Bookmark Topic Watch Topic
  • New Topic