• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

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: 24204
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: 76862
366
  • 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!
 
roses are red, violets are blue. Some poems rhyme and some are a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic