Source Code and Binaries Organization
- Every source code file must contain maximally one
publicclass. There may be more than one non-public classes.
- The name of the public class – if there is any – must be identical to the first part of the source code file.
- Source code file:
public class MyLove
- Partial classes are not allowed.
MyLove.classis created. The
.classfile contains the Java bytecode of the associated
.classfiles and ressources a
XYZ.jar(Java ARchive) can be created. A jar file is mainly a zip file of its contents, maybe with an added metadata file and the file extension
- In principle, a jar file is something similar to an assembly or dll in .NET. The term jar hell is well known to Java developers.
- A jar file can be a runnable jar file or a library.
jarcommandline program can be used to create or inspect jar files. It is a zip program with
jar tvf XYZ.jarshows a verbose content listing of XYZ.jar.
- In Eclipse, use
File - Export - Javato create a jar file.
Runnable Jar Files
- A runnable jar file is a an ordinary jar file with an added manifest file which contains an entry for the main class.
- The main class must have a
public static void main(String args)method. This method will be called when the jar is run.
- The manifest file is an ordinary UTF-8 encoded text file called
MANIFEST.MFlocated in the folder
META-INF. It can contain information about signing, version control, package sealing and others.
An entry for a main class may look like this
Gaming is the package name and
TraceOfDeathEngine is the class name.
Create a Runnable Jar, V1
- This method will pack all the files of the project into the jar file. There is no way here to select which files shall be included in the jar and which shall not. But as a jar file is an ordinary zip, you can remove superfluous files afterwards. Or see method 2.
- In Eclipse use
File - Export - Java - Runnable JAR fileto create an executable jar file.
- On the the following page, select your main class under
Launch configurationand input the file path of the jar file that shall be created.
Create a Runnable Jar, V2
- This method will pack only selected files of the project into the jar file.
- In Eclipse in the
Package Explorerselect the project or package which contains most of the stuff that shall be included in the jar.
File - Export - Java - JAR file. (Do not select
Runnable JARhere. You can create a runnable jar anyway.)
- On the the following page, select the Java- and resource files you want to have in the jar. Also set the export destination.
- On the fourth page, select
Generate the manifest fileat the top and at the bottom select your
Create a Runnable Jar, V3
If you don’t use Eclipse or want to automate creating a runnable jar file, you can use the
jar -cvfe troz.jar Gaming.TrodEngine Gaming
The command line above explained:
c: create new jar file v: be verbose f: specify archive file name e: pass entry point, create manifest automatically troz.jar: the archive file name Gaming.TrodEngine: the entry point is class `TrodEngine` in package `Gaming` Gaming: the folder to include in the jar.
If you have already a created manifest you can pass it to the
jar utility with the
Jar will then add it to the jar file as
jar cvfm trox.jar mf.txt Gaming
As said, the
jar utility program has
tar like syntax. So the line above means:
c: create new jar file v: be verbose f: specify archive file name m: include manifest file trox.jar: the archive file name mf.txt: the manifest file name Gaming: the folder to include in the jar.
Some quirks of the
- The archive file and the manifest file (or the entrys point) must be specified in the same order as the
- If any “file” is a directory then it is processed recursively.
- It is not allowed to split the options up.
tar cvf trox.jar m MAN Gamingis not possible.
Start a Java Program On Windows
On Windows, when Java is installed properly, you can doubleclick a runnable jar file from Windows Explorer to start it. Or you can create a shortcut and place it onto the desktop and use the shortcut to start the java program. You cannot attach such a shortcut to the taskbar, though.
To start a runnable
abc.jar via commandline and pass parameters param1 and param2 to the main classes main method, just call
java.exe -jar abc.jar param1 param2
You can also start a Java program if you don’t have a jar file, but only the needed
.class files. To do this Java.exe must be called from the bin directory one level above the package name. When the class to start is called
MyLove and resides in the package
Xcv and the parameters param1 and param2 shall be passed, the command line shall look like this:
java.exe Xcv.MyLove param1 param2
Create a Windows .exe File
To create an executable Windows exe file, you can use
launch4j. More details about
launch4j are given there (scroll down to method 3 of 3): http://www.wikihow.com/Create-an-Executable-File-from-Eclipse
There are several other possibilities to create a Windows .exe file, namely using
Java Web Start technology or an
Ahead Of Time compiler or …
A good overview about the available technologies is given here:
Jar files may be used as libraries and there are zillions of publicly available jar libraries.
This example shows you how to add a MySQL connector library to your Java project.
- Download the zipped Java connector for MySQL from there:
- Extract the package and copy just the
mysql-connector*.jarfile to somwhere below your workspace. I’d recommend a directory called
libwhich is a sibling of your
- In Eclipse, add the jar file to your lib path via
Project - Properties - Java Build Path - Libraries - Add External Jars...
- This will add the library to your project. Afterwards, you’ll see the library in the