Solved

problem compiling after organizing linux application source files into packages

Posted on 2004-09-08
12
194 Views
Last Modified: 2010-03-31
I was trying to organize my application into packages, but I'm not sure how.
Here is an example which matches my application structure
I have an application with a location path of
/main/myApp

inside the myApp directory
myMain.java  messageType1(directory)  messageType2(directory)

inside messageType1 directory
class1.java class2.java class3.java ..................

inside messageType2 directory
class1.java class2.java class3.java ..................


myMain.java uses all of the classes in both messageType sub-directories

what I want to do is make each messageType a package, so what I tried is adding
to the top of all .java source files in both message Type directories package names

in MessageType1 directory all source files have at the top
package messageType1;

in MessageType2 directory all source files have at the top
package messageType2;

in myMain.java I tried to import both packages by doing
import messageType1.*;
import messageType2.*;

I wasn't sure if myMain.java should have it's own package name and whether
I need to add the full path to the packages main.myApp.messageType1.*;
or if i just need to use javac and java with the right options.

so basically I don't know how to properly use packages
and compile and run them.

How can I make the classes in both sub-directories messageType1 and messageType2 packages imported by myMain.java and how do I compile and run them(how to set the sourcepath and classpath)

should myMain.java be a package as well?
both messageType1 and messageType2 are classes that will be used by other applications, there will be other messageTypes in the near future
0
Comment
Question by:mitchguy
  • 4
  • 4
  • 2
  • +2
12 Comments
 
LVL 92

Expert Comment

by:objects
ID: 12006831
put all your classes into packages, and I'd suggest using a common root for all package names

eg.

myApp.messageType1
myApp.messageType2
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12006860
>>in MessageType1 directory all source files have at the top
>>package messageType1;
OK

>>in MessageType2 directory all source files have at the top
>>package messageType2;
OK

>>in myMain.java I tried to import both packages by doing
>>import messageType1.*;
>>import messageType2.*;

if in your main you use class1 how will you know if it is class1 from the messageType1 or the messageType2 package?

Therefor you should

     import messageType1.class1;
     import messageType2.class2;
     import messageType1.class3;

Better:

avoid this:

>>inside messageType1 directory
>>class1.java class2.java class3.java ..................

>>inside messageType2 directory
>>class1.java class2.java class3.java ..................

Make it:

inside messageType1 directory
class11.java class12.java class13.java ..................

inside messageType2 directory
class21.java class22.java class23.java ..................
0
 
LVL 6

Expert Comment

by:expertmb
ID: 12006925
0
 

Author Comment

by:mitchguy
ID: 12006931
my messageType directories represent different message formats
inside each directory class1.java class2.java class3.java.............
each class here represents a different message in that format
messageType1 directory has about 75 files and messageType2 has about 30

so to apply what you are suggesting would I do:
in messageType1 directory
add to tops of classX.java

myApp.messageType1
or did you mean all classes, since each class is a message type?
myApp.messageType1.classX
0
 
LVL 6

Expert Comment

by:expertmb
ID: 12006973
good to organize the files in separate folder.
you can keep all the files in same folder, when you compile the classes generated will be put according to package structure.
0
 

Author Comment

by:mitchguy
ID: 12007009
>>zzynx
I apologize for not being more specific
my directories don't actually have
class1.java class2.java class3.java
they all have unique names representing a specific message
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 6

Expert Comment

by:expertmb
ID: 12007045
messagedirs\messageType1
                        \ClassMT11.java
                        \ClassMT12.java
                        .
                        .
                        .
                        \ClassMT1n.java
           \messageType2
                        \ClassMT21.java
                        \ClassMT22.java
                        .
                        .
                        .
                        \ClassMT2n.java
      
      
      in main.java
      
      import messageType1.*;
      import messageType2.*;
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12007078
>>I apologize for not being more specific
No problem :)

>>my directories don't actually have
>>class1.java class2.java class3.java
>>they all have unique names representing a specific message
Good. One doubt less ;°)
0
 

Author Comment

by:mitchguy
ID: 12007191
>>expertmb
>>messagedirs\messageType1
>>                       \ClassMT11.java
>>                       \ClassMT12.java

I currently have a common package label in all of the files
for example ClassMT11.java has at the top
package messageType1;

my main.java file which is in the directory above the class files
/main/myApp> ls
main.java  messageType1 messageType2

I have
import messageType1.*
import messageType2.*

and I tried compiling in the myApp directory using
javac -sourcepath /main/myApp/messageType1: /main/myApp/messageType2:  *.java
and I get the error messages
package messageType1 does not exist
package messageType2 does not exist
0
 
LVL 6

Accepted Solution

by:
expertmb earned 50 total points
ID: 12007454
try to compile one by one which dont have dependency, i mean main.java needs both packages to compile.

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12009706
You may find it much easier to use Ant once you've got the hang of it:

http://ant.apache.org/manual/CoreTasks/javac.html
0
 

Author Comment

by:mitchguy
ID: 12010589
>try to compile one by one which dont have dependency, i mean main.java needs both >packages to compile.

it seems at though if i compile each directory seperately then the program finds everything and it is compilable. I thought it would have compiled anything that it needed i.e. dependencies, I thought that was one of Java's features of not needing a makefile. Anyway problem solved
thanks
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now