Solved

problem compiling after organizing linux application source files into packages

Posted on 2004-09-08
12
196 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
ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

 

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java Loop 6 60
MySQL  on Tomcat 8 45
type mismatch (Object[] to double[] 4 24
Java: How do I open the default windows program for a given file type 3 28
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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

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