Skip to content
Snippets Groups Projects
Commit c714e257 authored by Nadav Har'El's avatar Nadav Har'El
Browse files

Improve error messages in "java.so -jar"

Print useful error messages, instead of cryptic exception traces,
in three cases of "java.so -jar something.jar":

 1. When something.jar doesn't exist
 2. When something.jar exists, but can't be read as a jar (zip) file
 3. When something.jar exists, but doesn't have a "Main-Class" field
    in its manifest.
parent dae261c5
No related branches found
No related tags found
No related merge requests found
import java.io.File; import java.io.File;
import java.io.FileNotFoundException;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.net.MalformedURLException; import java.net.MalformedURLException;
...@@ -7,6 +8,7 @@ import java.net.URLClassLoader; ...@@ -7,6 +8,7 @@ import java.net.URLClassLoader;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.jar.JarFile; import java.util.jar.JarFile;
import java.util.jar.Manifest; import java.util.jar.Manifest;
import java.util.zip.ZipException;
public class RunJava { public class RunJava {
...@@ -57,12 +59,24 @@ public class RunJava { ...@@ -57,12 +59,24 @@ public class RunJava {
static void runJar(String jarname, String[] args) throws Throwable { static void runJar(String jarname, String[] args) throws Throwable {
File jarfile = new File(jarname); File jarfile = new File(jarname);
JarFile jar = new JarFile(jarfile); try {
Manifest mf = jar.getManifest(); JarFile jar = new JarFile(jarfile);
jar.close(); Manifest mf = jar.getManifest();
String mainClass = mf.getMainAttributes().getValue("Main-Class"); jar.close();
setClassPath(jarname); String mainClass = mf.getMainAttributes().getValue("Main-Class");
runMain(loadClass(mainClass), args); if (mainClass == null) {
System.err.println(
"RunJava: No 'Main-Class' attribute in manifest of " +
jarname);
return;
}
setClassPath(jarname);
runMain(loadClass(mainClass), args);
} catch (FileNotFoundException e) {
System.err.println("RunJava: File not found: " + jarname);
} catch (ZipException e) {
System.err.println("RunJava: File is not a jar: " + jarname);
}
} }
static void runClass(String mainClass, String[] args) throws Throwable { static void runClass(String mainClass, String[] args) throws Throwable {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment