Title: Running Java Outside of Eclipse
Course: CPS 261: Advanced Java Concepts ~ DL & MM MASTER

Running Java Outside of Eclipse

Suppose that you want to run your program outside of the Eclipse environment. You will still need to make use of the Java tools installed on your computer.

It is helpful to have a specific program that we are talking about. So I'm going to start with the "Zip of Networked Game Collection" (currently in section 8). Download it and unzip it. There are multiple runnable programs here.

You will need to do 2 basic things:

1. Make sure that the command line program knows a "path" on how to find the java program. This part will depend on the computer you are on. At the bottom, I have some specifics for a number of platforms.
2. Make sure that the java classpath knows where to find any other classes necessary to make your program to work.

In the command line program, navigate to just inside the Networked Game Collection. From here you should be able to issue commands like:

java -classpath ./bin QuickClick.MyMain

or

java -classpath ./bin chess_networked_pixels.MyMain

Another approach: Jar file

Sometimes we like to have everything about the program in one file. In java this is often refered to as a "Jar" file. To demonstrate this, let's create a runnable Jar file for "QuickClick".

Right Click on MyMain.java in the QuickClick folder and choose "Export".

Open up the Java options and select "Runnable JAR file". Make sure the "Launch configuration" is correct and enter in a filename and location in the "Export destination". I would recommend a file that ends with ".jar".

This program you can be run with a command like:

java -jar QuickClick.jar

Note that if you are doing a program that has resources like images, I would recommend executing the previous command in a directory that contains the necessary resources.

By the way, you can also create the jar file using command line tools without using Eclipse. To do this, I would recommend navigating to the "bin" directory of your project. Issue the following command:

jar cvfe QuickClick.jar QuickClick.MyMain *

This command will leave a runnable QuickClick.jar file in the bin directory. The options on this command are as follows:

- c - create a jar file (there are also u, x, t options for updating, extracting and table of contents)
- v - verbose output ... if you leave this off, the command generates less information.
- f - Next thing on the command line will be the filename to be created
- e - Second thing on the command line will be the program Entry or starting point.

Platform specific "path" approaches

In this section we will look at how different platforms can find the java tools we need (javac, jar, java programs).

MAC:

My Mac is already setup to know where these programs are. A Mac has a unix platform at it's core. You can go to a terminal window and check out how this is done.

To see where programs can normally be found issue the command:

echo $PATH

on my MAC, I get

/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin

These are the normal directories where programs can be found. When I issue the command:

whereis java

I get:

/usr/bin/java

This happens to be a linked (or aliased ) pathname. To see where the file really lives you can issue the command:

ls -l /usr/bin/java

You will then see where java really lives.

Windows PC:

I am currently running on Windows 7, and this system seems to know where my latest "java" program is. To prove this for your system, go to a command line window and issue the command:

java -version

To see the Windows "path" you can issue the command:

path

I get output something like:

C:\Users\chasselb.000>path
PATH=C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Windows\system32;
C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;.......

On my system, I found the "java" program in the C:\Windows\system32 directory.

I found this by doing a File Search and when "java" was found, I did a Right-Click properties on it to discover where it lived.

Note that you can see some related environment variables (JAVA_HOME, and Path) in System Control Panel entry.

Last Updated: April 4, 2015 2:49 AM