Win a copy of High Performance Python for Data Analytics this week in the Python forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

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: 24203
44
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: 71769
312
  • 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!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic