0

I have a launcher I'm creating as a mini side project and I'm trying to execute a jar file. I figured out how to execute a exe. Heres the code:

mineshaft.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String mhome = System.getProperty("user.home") + ""; try { Runtime.getRuntime().exec("java -jar .mhome+ \\AppData\\Roaming\\.minecraft\\minecraft.exe"); } catch (Exception a) { } } }); 

Ive searched everywhere, and everyone says this code works, but it won't for me. Any suggestions?

6
  • 1
    Put a a.printStacktrace() in your catch statement. Are you getting any errors/exceptions? Commented Sep 13, 2012 at 1:00
  • 1
    .mhome+ doesn't belong inside the quoted string. Commented Sep 13, 2012 at 1:00
  • Is minecraft.exe actually a jar file? Typically executable jar files have a .jar extension. Commented Sep 13, 2012 at 1:03
  • oh wow meant mineshaft.jar sorry. I copied some code from other sections of my program Commented Sep 13, 2012 at 1:04
  • That wasn't the answer though... Hopefully someone can help me Commented Sep 13, 2012 at 1:25

2 Answers 2

1

First, your command is wrong. .mhome+ ? Make sure your command is correct. From a command prompt enter the following:

java -jar .mhome\AppData\Roaming\.minecraft\minecraft.exe 

That's essentially what you are running.

Also, PRINT YOUR STACKTRACE. Especially during development.

I think you are looking for something like this:

 public void actionPerformed(ActionEvent e) { String mhome = System.getProperty("user.home") + ""; try { Runtime.getRuntime().exec("java -jar " + mhome + "\\AppData\\Roaming\\.minecraft\\minecraft.jar"); } catch (Exception a) { a.printStrackTrace(); } } 
Sign up to request clarification or add additional context in comments.

Comments

0
JarInputStream stream = null; try { File jar = new File("/path/to/jar.jar"); stream = new JarInputStream(new FileInputStream(jar)); String mainClass = stream.getManifest().getMainAttributes().getValue("Main-class"); ClassLoader loader = URLClassLoader.newInstance(new URL[] { jar.toURI().toURL() }, getClass().getClassLoader); Class<?> main = Class.forName(mainClass, loader); main.getDeclaredMethod("main", String[].class).invoke(null, null); } finally { stream.close(); } 

km1's answer is simpler if it suits your needs.

Comments