Source Code and Binaries Organization
Source Code
- Every source code file must contain maximally one
public
class. 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:
MyLove.java
->public class MyLove
- Partial classes are not allowed.
Binaries
- From
MyLove.java
aMyLove.class
is created. The.class
file contains the Java bytecode of the associated.java
file. - From
.class
files and ressources aXYZ.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.jar
instead of.zip
. - 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.
- The
jar
commandline program can be used to create or inspect jar files. It is a zip program withtar
like syntax. jar tvf XYZ.jar
shows a verbose content listing of XYZ.jar.- In Eclipse, use
File - Export - Java
to 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.MF
located in the folderMETA-INF
. It can contain information about signing, version control, package sealing and others.
An entry for a main class may look like this
Main-Class: Gaming.TraceOfDeathEngine
Here, 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 file
to create an executable jar file. - On the the following page, select your main class under
Launch configuration
and 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 Explorer
select the project or package which contains most of the stuff that shall be included in the jar. - Use
File - Export - Java - JAR file
. (Do not selectRunnable JAR
here. 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 file
at the top and at the bottom select yourMain class
.
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
utility.
An example:
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 m
option. Jar
will then add it to the jar file as META-INF/MANIFEST.MF
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 jar
utility:
- The archive file and the manifest file (or the entrys point) must be specified in the same order as the
f
,m
ande
flags. - 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 Gaming
is 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:
http://www.excelsior-usa.com/articles/java-to-exe.html
Libraries
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:
http://dev.mysql.com/downloads/connector/j. SelectPlatform Independent
andZIP Archive
. - Extract the package and copy just the
mysql-connector*.jar
file to somwhere below your workspace. I’d recommend a directory calledlib
which is a sibling of yourbin
directory. - 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
Package Explorer
underReferenced Libraries
.