File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Protected

 
Shashank Gokhale
Ranch Hand
Posts: 92
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the two following classes that are in their own files. I am trying to get package1class2 to access package1class1 but for some reason I get compiler errors from trying to compile package1class2, all of which refer to package1class1

I can successfully compile package1class1.java, but not the other one. Both classes are in the same package, so even though one is not a subclass of the other, package1class2 should still be able to reference package1class1. What is going wrong?
I initially thought that these two classes had to be subclasses in addition to being in the same package, but the section under PROTECTED in this tutorial says they dont have to be related by inheritance if they are members of the same package.
 
Rich Raposa
Ranch Hand
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shashank,
Your code is perfectly fine. I'm not sure what your compiler error is, but I suspect it has something to do with either classpath or not having your .class file in the appropriate file structure.
Try this:
1. Delete the .class files (if you got one of them to compile successfully).
2. Compile both classes using the -d flag, and you can compile them at the same time. The command will look something like:
javac -d . *.java
3. To run the program, be sure to use the package name, as in:
java package1.package1class2
Your classes compiled and ran fine on my machine.
Good luck,
Rich
 
Shashank Gokhale
Ranch Hand
Posts: 92
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the Java compiler for 1.4 downloaded from the java page at the sun site. SWo why do my classes not compile when what I have is almost the same as what they have on their page
I havent had any problems with classpaths or anything so far so Im pretty thats not it.
The compiler errors I have when trying to compile package1class2.java are
package1class2:java:7: Cannot resolve symbol
symbol: class package1class1
location: class package1.package1class2()
package1class1 paca=new package1class1();
package1class2:java:7: Cannot resolve symbol
symbol: class package1class1
location: package package1
package1class1 paca=new package1class1();
package1class2:java:12: Cannot resolve symbol
symbol: class package1class1
location: class package1.package1class2()
package1class1 p1c1=new package1.package1class1();
package1class2:java12: Cannot resolve symbol
symbol: class package1class1
location: package package1
package1class1 p1c1=new package1class1();
So why is package1class2 unable to see package1class1?
[ February 27, 2003: Message edited by: Shashank Gokhale ]
[ February 27, 2003: Message edited by: Shashank Gokhale ]
 
Rich Raposa
Ranch Hand
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your compiler errors are clearly related to classpath. I copy-and-pasted your code onto my PC, compiled and executed it with no problem.
Trust me here and perform the following steps:
1. Delete any bytecode (.class) files you currently have.
2. Enter the following from the command prompt:
javac -d . *.java
3. Type in the following:
set classpath=.;
4. Run the program:
java packagename.ClassName
That should do it!
Rich
 
Shashank Gokhale
Ranch Hand
Posts: 92
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rich,
I have been able to compile and execute the classes since the last time I posted here. The only thing is that they compile succesffully when I run the javac compiler from the parent directory of the directory (package) that contains these classes. Is this always true, that classes in packages can only be compiled from outside the immediate directory?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic