Tag Archives: java

Java: create executable jar file with eclipse

The version of eclipse is:

Eclipse Java EE IDE for Web Developers.

Version: Indigo Service Release 1
Build id: 20110916-0149

At first, I use “export” to make a match.jar file, and ran it on Dos with:

java -jar match.jar

It shows the error:

Exception in thread “main” java.lang.NullPointerException
at sun.launcher.LauncherHelper.getMainClassFromJar(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)

It’s said that it needs manifest file to define an entry for main class.

The manifest.mf should look like this:

Manifest-Version: 1.0
Main-Class: byjson.Match

But soon, I found that I should choose to make a “exacutable jar” on eclipse, don’t need to make manifest file manually. The process is as below:

1) Right click the project name on the project explorer; then choose “Export” on the popup menu.

2) then click “Java” folder in the popup window, choose “runnable JAR file”

Here I choose “Package required libraries into generated JAR”, probably other choices are also ok.

Leave the space for “Save as ANT script” empty.

Click “Finish”.

Under DOS, run the match.jar with the command:

java -jar match.jar

It works fine.

Warning: Vector is a raw type. References to generic type Vector should be parameterized

When I wrote the code as below in Eclipse:

Vector listings_vec = new Vector(3, 2);

It has a warning:

Vector is a raw type. References to generic type Vector should be parameterized

Simply, I changed it to the code as below:

Vector listings_vec = new Vector(3, 2);

The warning disappeared.

It’s said that it’s about “generics”. Java 1.5 is the first version that introduced “generics”. It helps to make type safety in the code.

Sorting a 2-dimensional array in java

These codes sort a 2-dimensional array. It has 8 columns. The rows are sorted by the 5th column from high to low. It can be a reference for sorting a multi-column array.

/*

length is length of the sorted array
final_seven is the array.

*/

String[] temp = new String[8];

if (final_seven[0] == null) return “no result”;

for (int m = 1; m < length; m++) {                    
for (int n = m; n >= 1 ; n–) {

if( Integer.parseInt(final_seven[n][4]) > Integer.parseInt(final_seven[n-1][4]) ) {

temp = final_seven[n];
final_seven[n] = final_seven[n-1];
final_seven[n-1] = temp;

} else break;
}
}