Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 204
  • Last Modified:

problem compiling after organizing linux application source files into packages

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
mitchguy
Asked:
mitchguy
  • 4
  • 4
  • 2
  • +2
1 Solution
 
objectsCommented:
put all your classes into packages, and I'd suggest using a common root for all package names

eg.

myApp.messageType1
myApp.messageType2
0
 
zzynxSoftware engineerCommented:
>>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
 
expertmbCommented:
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!

 
mitchguyAuthor Commented:
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
 
expertmbCommented:
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
 
mitchguyAuthor Commented:
>>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
 
expertmbCommented:
messagedirs\messageType1
                        \ClassMT11.java
                        \ClassMT12.java
                        .
                        .
                        .
                        \ClassMT1n.java
           \messageType2
                        \ClassMT21.java
                        \ClassMT22.java
                        .
                        .
                        .
                        \ClassMT2n.java
      
      
      in main.java
      
      import messageType1.*;
      import messageType2.*;
0
 
zzynxSoftware engineerCommented:
>>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
 
mitchguyAuthor Commented:
>>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
 
expertmbCommented:
try to compile one by one which dont have dependency, i mean main.java needs both packages to compile.

0
 
CEHJCommented:
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
 
mitchguyAuthor Commented:
>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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 4
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now