Solved

problem compiling after organizing linux application source files into packages

Posted on 2004-09-08
12
197 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Way to decrease size of apk file 9 104
by zero exception 10 69
jsp login check 12 52
DTD and JAVA versions 1 26
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

679 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