Solved

A Package spread over multiple files??

Posted on 2004-09-18
9
345 Views
Last Modified: 2010-03-31
1).Can a JAVA package be spread over multiple files?
2).If yes,how do i name those files which have classes that do not have the main() in them?
3).How about naming those files?
4).How should I use the jar utility to make an archive out of all those files?

This will be of great help, experts.
0
Comment
Question by:perfect_tranquility
9 Comments
 
LVL 3

Accepted Solution

by:
sbockelman earned 125 total points
Comment Utility
1. Yes.  In fact, they almost HAVE to be, since you can only have one public class per file.
            I even have packages that span JARs.  Packaging is accomplished entirely by the  package statement at the top of each source file.

2.  You must name each file containing a public class by the class name + ".java" extension.
     You need to use the import statement in the class containing main, then just use the classes.

3.  See #2.

4.  See the documentation for th jar utility at java.sun.com.  Or run jar --help for the options.

0
 

Author Comment

by:perfect_tranquility
Comment Utility
Regarding #1,

I am aware that there can only be one public class per file.However,why would I want to have multiple public classes when i would need just one main() function?
0
 
LVL 2

Assisted Solution

by:Breadstick
Breadstick earned 125 total points
Comment Utility
4.  First create a file called manifest.mf.  That file should contain something like the following:

Main-Class: MyProgram


Be sure to leave an extra blank line after declaring Main-Class.  Replace 'MyProgram' with the class file you would like to run.  The one containing the main() method.  If your program is part of a package, your Main-Class will look something like this: com.cyntaks.MyProgram

If you're program is not part of a package, save manifest.mf to the directory containing your class files.  If it is part of a package, say com.cyntaks, save manifest.mf to the directory that the com folder is in.

To create the jar file, open up your command prompt and cd to the directory that you saved manifest.mf to.  If your program is not part of a package, type:
jar cfm "MyJarFile.jar" manifest.mf *

If you're program is part of a package, say com.cyntaks, type:
jar cfm "MyJarFile.jar" manifest.mf com

You can replace MyJarFile.jar with whatever you want the jar file to be created to be called.  To run your jar file, type:
java -jar MyJarFile.jar

Or if you're on windows you can generally just double-click it.

----------------

>   I am aware that there can only be one public class per file.
>   However,why would I want to have multiple public classes
>   when i would need just one main() function?

Sometimes a jar package will not even contain a class with a main() method.  It may just contain classes that can be used to add functionality to a program that uses it.  You don't need to have more than one class per jar file.  Java code is sometimes split over multiple files for organization, file size, etc...
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 2

Expert Comment

by:Breadstick
Comment Utility
>   2.  You must name each file containing a public class by the class name + ".java" extension.
>   You need to use the import statement in the class containing main, then just use the classes.

This is generally true.  However, if you're program is not part of a package, and the rest of the classes you've written for it are in the same directory, then you do not have to import the other classes, as they will already be part of the classpath when you run your program.
0
 

Assisted Solution

by:orajava
orajava earned 125 total points
Comment Utility
1).Can a JAVA package be spread over multiple files?

     Yes..The java package can be spread over multiple files...
      But, Its entitely ur choice that you can keep one file in a package or multiple.

      a)  Packages are used to resolve name space conflicts amongs the java files having same name
          in case of lage stuctured software.

      b) Packages are also used to keep a set of programs solving a particular problem in one physical
          and logical name-space, which can be understood by JRE/JVM.

2).If yes,how do i name those files which have classes that do not have the main() in them?
3).How about naming those files?

       Following example i am citing is for naming any file with or without main under packages:

       The point is simple you can only execute the class file with main using java ..others
        will be called by the class containing the main() method.But they will be in the same
        package.

       To name files which do not have main() method in them...

       You have to write the following on the top of program:
       
         package <package-name> // eg: com.module.sales
         
         import  <all-files-you-want-to-import> // can be more than one eg. java.io.*

          class   <name-of-class> // need not be public
            {// all the other codes will come here
                      }
        Name  the file "<name-of-class>.java"

        You have to keep this file in directory named "<package-name>"  
        // eg. /com/modules/sales/<name-of-class>.java

        if you set your class-path to the directory in which directory "<package-name>"
        //  eg: "/usr/home/raju" this has directory "com"

        now you can execute you this file ( assuming it to be ur main file) by calling
        "java <package-name>.<name-of-class>"

               
3).How should I use the jar utility to make an archive out of all those files?

      Jar is packaging and archiving tool for java...

       you can use in two ways..

      a) usage  "jar -cvf Pattern.jar PatternMaker.class Patterns.class"

         this will compress the class files into "Pattern.jar" ..and this can be excuted as it can
         itself identify the main class in itself and execute it..

       b)  usage "jar -cf project.jar project"

           this will compress and archive all classes in the project directory..
           which will be your package(rember <package-name> from prev question)..and thus you need not carry
           ..separate class files you can do is just use this jar archive for executing that package or access
           anything in that package (if given in class-path)
           
            The java JVM/JRE understands the jar format and thus we can use jar to package modules together..
             even we can package whole application using jar..

            It makes the handling of the class files easier when downloading or deploying application.


          Hope this satifies your questions....
0
 
LVL 15

Assisted Solution

by:Javatm
Javatm earned 125 total points
Comment Utility
> 1).Can a JAVA package be spread over multiple files?
> 2).If yes,how do i name those files which have classes that do not have the main() in them?
> 3).How about naming those files?
> 4).How should I use the jar utility to make an archive out of all those files?

This will be all my answers :

1. Yes because thats the purpose of why you would like to package.
2. Still .java it doesnt matter if they have a main or not if you make a jar file it will already be archived
3. All java files that you created needed not to be renamed.
4. Well you can learn how to create a perfect jar file by reading this :  

http://java.sun.com/docs/books/tutorial/jar/basics/

PS : 1 Jar file is equal to one application and you can only run the main class of that application.

Here is an old example of how you can create a Jar file easily :
http://www.experts-exchange.com/Q_20774096.html

Hope that helps . . .
Javatm
0
 
LVL 15

Expert Comment

by:Javatm
Comment Utility
And one another thing on Netbeans IDE that I suggested on your previous questions has a utility to make a jar file for
you so that you wont have to worry about it.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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…
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…

772 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

15 Experts available now in Live!

Get 1:1 Help Now