mitchguy
asked on
compiling, executing problems after re-structuring program into packages
I've broken my application, because I don't fully understand how to use packages
I've tried to make a test program with the same structure and can't get it to work either.
My question is how to get my test program to work, so I can apply the solution to my broken application.
Here is the test program:
my directory structure is
/main/packageExample/topPa ckage/subP ackage/sub SubPackage
topPackage directory contains one file
Top.java
subPackage directory contains one file
Sub.java
subSubPackage directory contains one file
SubSub.java
I want to run Top , which will call Sub, which will call SubSub, where
Top is in package topPackage
Sub is in a subPackage (sub package of topPackage)
and SubSub is in subSubPackage (a sub package of subPackage)
Here is what I came up with for the contents of these 3 files
FILE: Top.java
----------------
package topPackage;
import java.io.*;
import subPackage.*;
public class Top
{
public static void main(String[] args) throws IOException
{
Top top = new Top( );
}
public Top( )
{
topString = new String("Hello Top");
sub = new Sub( );
}
String topString;
Sub sub;
}
FILE: Sub.java
-------------------------- --
package topPackage.subPackage;
import subSubPackage.*;
public class Sub
{
public Sub( )
{
subString = new String("Hello Sub");
subSub = new SubSub( );
}
String subString;
SubSub subSub;
}
FILE: SubSub.java
-------------------------- --
package topPackage.subPackage.subS ubPackage;
public class SubSub
{
public SubSub( )
{
subSubString = new String("Hello SubSub");
}
String subSubString;
}
I've tried to make a test program with the same structure and can't get it to work either.
My question is how to get my test program to work, so I can apply the solution to my broken application.
Here is the test program:
my directory structure is
/main/packageExample/topPa
topPackage directory contains one file
Top.java
subPackage directory contains one file
Sub.java
subSubPackage directory contains one file
SubSub.java
I want to run Top , which will call Sub, which will call SubSub, where
Top is in package topPackage
Sub is in a subPackage (sub package of topPackage)
and SubSub is in subSubPackage (a sub package of subPackage)
Here is what I came up with for the contents of these 3 files
FILE: Top.java
----------------
package topPackage;
import java.io.*;
import subPackage.*;
public class Top
{
public static void main(String[] args) throws IOException
{
Top top = new Top( );
}
public Top( )
{
topString = new String("Hello Top");
sub = new Sub( );
}
String topString;
Sub sub;
}
FILE: Sub.java
--------------------------
package topPackage.subPackage;
import subSubPackage.*;
public class Sub
{
public Sub( )
{
subString = new String("Hello Sub");
subSub = new SubSub( );
}
String subString;
SubSub subSub;
}
FILE: SubSub.java
--------------------------
package topPackage.subPackage.subS
public class SubSub
{
public SubSub( )
{
subSubString = new String("Hello SubSub");
}
String subSubString;
}
I would write the following (but don't know if that's the solution):
FILE: Top.java
----------------
package topPackage;
import java.io.*;
import topPackage.subPackage.*;
...
FILE: Sub.java
-------------------------- --
package subPackage;
import topPackage.subPackage.subS ubPackage. *;
...
FILE: SubSub.java
-------------------------- --
package subSubPackage;
...
FILE: Top.java
----------------
package topPackage;
import java.io.*;
import topPackage.subPackage.*;
...
FILE: Sub.java
--------------------------
package subPackage;
import topPackage.subPackage.subS
...
FILE: SubSub.java
--------------------------
package subSubPackage;
...
What's the problem? Compiling or running?
I guess running, as that should all compile ok...
cd /main/packageExample/
java -cp . topPackage.Top
I guess running, as that should all compile ok...
cd /main/packageExample/
java -cp . topPackage.Top
>> I've broken my application
What (compiler or runtime) errors do you get?
What (compiler or runtime) errors do you get?
> as that should all compile ok...
Ahhhh! Nope, I think zzynx has hit the nail on the head...
Your imports are wrong...
Ahhhh! Nope, I think zzynx has hit the nail on the head...
Your imports are wrong...
imports need to be absolute, not relative (as zzynx said) :-)
ASKER
I added the absolute paths
FILE: Sub.java
-------------------------- --
package topPackage.subPackage;
import topPackage.subPackage.subS ubPackage. *;
public class Sub
{
public Sub( )
{
subString = new String("Hello Sub");
subSub = new SubSub( );
}
String subString;
SubSub subSub
}
FILE: Top.java
----------------
package topPackage;
import java.io.*;
import topPackage.subPackage.*;
public class Top
{
public static void main(String[] args) throws IOException
{
Top top = new Top( );
}
public Top( )
{
topString = new String("Hello Top");
sub = new Sub( );
}
String topString;
Sub sub;
}
when I try to compile Top.java I get errors
package topPackage.subPackage does not exist
cannot resolve symbol Sub
when I try to compile Sub.java I get errors
package topPackage.subPackage.subS ubPackage does not exist
cannot resolve symbol SubSub
when I try to compile SubSub.java it compiles fine
FILE: Sub.java
--------------------------
package topPackage.subPackage;
import topPackage.subPackage.subS
public class Sub
{
public Sub( )
{
subString = new String("Hello Sub");
subSub = new SubSub( );
}
String subString;
SubSub subSub
}
FILE: Top.java
----------------
package topPackage;
import java.io.*;
import topPackage.subPackage.*;
public class Top
{
public static void main(String[] args) throws IOException
{
Top top = new Top( );
}
public Top( )
{
topString = new String("Hello Top");
sub = new Sub( );
}
String topString;
Sub sub;
}
when I try to compile Top.java I get errors
package topPackage.subPackage does not exist
cannot resolve symbol Sub
when I try to compile Sub.java I get errors
package topPackage.subPackage.subS
cannot resolve symbol SubSub
when I try to compile SubSub.java it compiles fine
In my comment I removed the "paths" in the lines
package xxxx;
package xxxx;
>> when I try to compile SubSub.java it compiles fine
Did you try compiling Sub.java hereafter?
And then Top.java after that?
Did you try compiling Sub.java hereafter?
And then Top.java after that?
ASKER
I just removed the paths in the lines package xxxx;
and still get the same thing
I have tried recompiling Sub.java after compiling SubSub.java
still get errors
package topPackage.subPackage.subS ubPackage does not exist
cannot resolve symbol SubSub
and still get the same thing
I have tried recompiling Sub.java after compiling SubSub.java
still get errors
package topPackage.subPackage.subS
cannot resolve symbol SubSub
cd /main/packageExample/
javac -cp . topPackage/Top.java topPackage/subPackage/Sub. java topPackage/subPackage/subS ubPackage/ SubSub.jav a
javac -cp . topPackage/Top.java topPackage/subPackage/Sub.
ASKER
I did
cd /main/packageExample/
javac -classpath . topPackage/Top.java topPackage/subPackage/Sub. java topPackage/subPackage/subS ubPackage/ SubSub.jav a
I get the errors:
./topPackage/subPackage/Su b.java:5: duplicate class: subPackage.Sub
public class Sub
^
topPackage/Top.java:20: cannot access topPackage.subPackage.Sub
bad class file: ./topPackage/SubPackage/Su b.java
file does not contain class topPackage.subPackage.Sub
Please remove or make sure it appears in the correct subdirectory of the classpath
Sub sub;
^
2 errors
I am compiling on a linux OS by the way, I don't think that matters
cd /main/packageExample/
javac -classpath . topPackage/Top.java topPackage/subPackage/Sub.
I get the errors:
./topPackage/subPackage/Su
public class Sub
^
topPackage/Top.java:20: cannot access topPackage.subPackage.Sub
bad class file: ./topPackage/SubPackage/Su
file does not contain class topPackage.subPackage.Sub
Please remove or make sure it appears in the correct subdirectory of the classpath
Sub sub;
^
2 errors
I am compiling on a linux OS by the way, I don't think that matters
ASKER
Are you able to compile everything using what you suggested and it's just me???
>>cd /main/packageExample/
>>javac -cp . topPackage/Top.java topPackage/subPackage/Sub. java >>topPackage/subPackage/su bSubPackag e/SubSub.j ava
>>cd /main/packageExample/
>>javac -cp . topPackage/Top.java topPackage/subPackage/Sub.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Try removing all *.class files they may be conflicting with the new package structure...
Tim
Tim
ASKER
I had a mismatch of combinations, from the suggested solutions and I was compiling wrong.
I used your compilation suggestion, but with the paths removed from the package statements and then I put the paths back in, but then only compiled with javac *.java, which is the way I learned to compile things, which is obviously not good for bigger applications
I got it compiled and running now
can you explain the line
>>java -cp . topPackage.Top
I've never compiled like that before
Thanks
I used your compilation suggestion, but with the paths removed from the package statements and then I put the paths back in, but then only compiled with javac *.java, which is the way I learned to compile things, which is obviously not good for bigger applications
I got it compiled and running now
can you explain the line
>>java -cp . topPackage.Top
I've never compiled like that before
Thanks
mitchguy, sorry if it looked like if I wasn't interested anymore to help you, but I had to go offline.
But with Tim you were in safe hands. ;°)
But with Tim you were in safe hands. ;°)
> But with Tim you were in safe hands. ;°)
:-)
Good luck mitchguy!!
Tim
:-)
Good luck mitchguy!!
Tim
ASKER
when I instanstiate Top( )