Solved

problem compiling after organizing linux application source files into packages

Posted on 2004-09-08
12
199 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Suggested Courses

623 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