aspose file tools*
The moose likes Beginning Java and the fly likes Protected Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Protected " Watch "Protected " New topic
Author

Protected

Shashank Gokhale
Ranch Hand

Joined: Jan 07, 2003
Posts: 92
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.


May the force of the Java be in all of us !!!
Rich Raposa
Ranch Hand

Joined: Dec 06, 2001
Posts: 46
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


Rich Raposa<br /><a href="http://www.javalicense.com" target="_blank" rel="nofollow">http://www.javalicense.com</a><br />Buy Java courseware -> get a free XBox!
Shashank Gokhale
Ranch Hand

Joined: Jan 07, 2003
Posts: 92
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

Joined: Dec 06, 2001
Posts: 46
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

Joined: Jan 07, 2003
Posts: 92
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?
 
 
subject: Protected