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
Solved

A Package spread over multiple files??

Posted on 2004-09-18
9
351 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
ID: 12092910
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
ID: 12092926
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
ID: 12092951
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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 2

Expert Comment

by:Breadstick
ID: 12093004
>   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
ID: 12093710
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
ID: 12093855
> 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
ID: 12093862
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to implement Singleton Design Pattern in Java.

809 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