I`ve been looking at an application for linux that is written in java - not some kind of java plugin for a web browser, but a self-contained programme. So these are just some notes on running a java application on linux - they are based on SuSe Linux 11.0, with the Gnome desktop.
Java is a high level programming language, not a thousand miles away from C. Like C, a programmer writes the source code, and then this is compiled to produce a lower level code, that runs on a seperate software layer known as a Java Runtime Environment ( JRE ) or Java Virtual Machine ( JVM ).
The JRE layer is custom designed for each type of operating system that it needs to run on, eg, MS Windows, Linux, etc, and each version of the JRE presents to the java application an identical interface - so the same java application will run on MS Windows, Linux, etc.
The default build of SuSe Linux 11 includes the Sun JRE version 1.6, however if your linux does not include a JRE, it can be downloaded from the Sun website for free.
So far I have come across four types of java files :-
You can tell if a .jar file is executable by looking inside it with a programme such as File Roller, and looking for the header files - in a folder called "META-INF" - inside this folder there will be some header files, including one called "MANIFEST.MF".
The "MANIFEST.MF" file may contain various amounts of information, but should contain one specific line, which looks like
Main-Class: <filename>
The file <filename> is the name of the .class file which is the starting point for running the application, and this tells the JRE which .class file to start with, out of all the .class files inside the .jar file.
Other information that may be in the "MANIFEST.MF" file includes :-
Executable .jar files can be run either from the Gnome Terminal command line shell, or from a new icon on the Gnome desktop. The required command line is
java -jar /<full-path>/<filename>.jar
Note that the .jar file extension is required.
This line will work in both the command line shell and in a desktop icon. The full path to the <filename>.jar file should be included.
If the java files are not inside a .jar file, then you can run the application by pointing the JRE to the Main-Class file. The simplest case is when you are "in" the folder containing the Main-Class file, and this folder is the root folder for the java application - this of course means that you could really only do this whilst working within a command line shell. The command would be :-
java <filename>
Note that the .class file extension is not included.
This command line is actually a default abbreviation for the more complete command line instruction, which is :-
java -classpath . <filename>
where the "-classpath" defines the path to the folder containing the <filename> file - the "." signifies the "current directory".
This command line can be abbreviated to
java -cp . <filename>
Now that we`ve got a way of defining the path, it means that we don`t have to be "in" the folder containing the .class file. So we can put in full paths :-
java -classpath /stuff/java/this-app <filename>
So we can use this either within a command line shell, or within an icon on the desktop. If the .class file we want to run is not in the root directory for the application, but in a sub-directory, then the <filename> part of this command line should include the relative path to the <filename> file - and the classpath part should point to the application root directory :-
java -classpath /stuff/java/this-app <sub-directory>/<filename>
If we don`t want to include the "classpath" part in the command line to run the application, we can set up a variable called "CLASSPATH". The command line for this would be
CLASSPATH=/stuff/java/this-app export CLASSPATH
and this would be run at the command line or put into a file, depending on what kind of variable that you want to set up.
If you want more information on setting variables in SuSe Linux 11, then here is a web page about it.
Note that if you do set up a "CLASSPATH" variable, another classpath instruction in the command line to run the application will over-ride the path contained in the variable "CLASSPATH".