Solved

compiling, executing problems after re-structuring program into  packages

Posted on 2004-09-20
18
217 Views
Last Modified: 2012-05-05
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/topPackage/subPackage/subSubPackage

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.subSubPackage;

public class SubSub
{
  public SubSub( )
  {
    subSubString = new String("Hello SubSub");
  }
 String subSubString;
}

0
Comment
Question by:mitchguy
  • 7
  • 6
  • 5
18 Comments
 

Author Comment

by:mitchguy
ID: 12102586
I forgot to add println's to each constructor, I wanted to print out each hello string
when I instanstiate Top( )
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12102591
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.subSubPackage.*;
...

FILE: SubSub.java
----------------------------
package subSubPackage;
...
0
 
LVL 35

Expert Comment

by:TimYates
ID: 12102607
What's the problem?  Compiling or running?

I guess running, as that should all compile ok...

cd /main/packageExample/
java -cp . topPackage.Top
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 37

Expert Comment

by:zzynx
ID: 12102614
>> I've broken my application
What (compiler or runtime) errors do you get?
0
 
LVL 35

Expert Comment

by:TimYates
ID: 12102619
> as that should all compile ok...

Ahhhh!  Nope, I think zzynx has hit the nail on the head...

Your imports are wrong...
0
 
LVL 35

Expert Comment

by:TimYates
ID: 12102622
imports need to be absolute, not relative (as zzynx said) :-)
0
 

Author Comment

by:mitchguy
ID: 12102823
I added the absolute paths

FILE: Sub.java
----------------------------

package topPackage.subPackage;
import topPackage.subPackage.subSubPackage.*;

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.subSubPackage does not exist
cannot resolve symbol SubSub

when I try to compile SubSub.java it compiles fine
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12102843
In my comment I removed the "paths" in the lines

       package xxxx;
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12102877
>> when I try to compile SubSub.java it compiles fine
Did you try compiling Sub.java hereafter?
And then Top.java after that?
0
 

Author Comment

by:mitchguy
ID: 12102934
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.subSubPackage does not exist
cannot resolve symbol SubSub
0
 
LVL 35

Expert Comment

by:TimYates
ID: 12102983
cd /main/packageExample/
javac -cp . topPackage/Top.java topPackage/subPackage/Sub.java topPackage/subPackage/subSubPackage/SubSub.java
0
 

Author Comment

by:mitchguy
ID: 12103100
I did
cd /main/packageExample/
javac -classpath . topPackage/Top.java topPackage/subPackage/Sub.java topPackage/subPackage/subSubPackage/SubSub.java

I get the errors:
./topPackage/subPackage/Sub.java:5: duplicate class: subPackage.Sub
public class Sub
         ^

topPackage/Top.java:20: cannot access topPackage.subPackage.Sub
bad class file: ./topPackage/SubPackage/Sub.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
0
 

Author Comment

by:mitchguy
ID: 12103234
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/subSubPackage/SubSub.java


0
 
LVL 35

Accepted Solution

by:
TimYates earned 100 total points
ID: 12103356
Here are the files I'm compiling:
------------------------

package topPackage.subPackage.subSubPackage;

public class SubSub
{
  public SubSub( )
  {
    System.out.println("Hello SubSub");
  }
 String subSubString;
}

----------------------

package topPackage.subPackage;
import topPackage.subPackage.subSubPackage.*;

public class Sub
{
  public Sub( )
  {
    System.out.println("Hello Sub");
    subSub = new SubSub( );
  }
 String subString;
 SubSub subSub;
}

-------------------------------

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( )
  {
    System.out.println( "Hello Top" ) ;
    sub = new Sub( );
  }
 String topString;
 Sub sub;
}

-------------------------------

They're in this structure:

[tyates@linux java]$ find topPackage/ -name *.java
topPackage/subPackage/subSubPackage/SubSub.java
topPackage/subPackage/Sub.java
topPackage/Top.java

-------------------------------

they compile like this:

[tyates@linux java]$ javac -classpath . topPackage/Top.java topPackage/subPackage/Sub.java topPackage/subPackage/subSubPackage/SubSub.java

--------------------------------

And run like this:

[tyates@linux java]$ java -cp . topPackage.Top
Hello Top
Hello Sub
Hello SubSub
0
 
LVL 35

Expert Comment

by:TimYates
ID: 12103366
Try removing all *.class files  they may be conflicting with the new package structure...

Tim
0
 

Author Comment

by:mitchguy
ID: 12104178
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

0
 
LVL 37

Expert Comment

by:zzynx
ID: 12109563
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. ;°)
0
 
LVL 35

Expert Comment

by:TimYates
ID: 12109953
> But with Tim you were in safe hands. ;°)

:-)

Good luck mitchguy!!

Tim
0

Featured Post

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Weekend adv creator 3 38
nextBoolean(double p) for Random class 3 40
JavaFX TableView not displaying correctly 3 17
Is there a simpler dropbox system? 10 20
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question