0

My Problem is, that i can't open a jar in a Java Program. The .jar that i want to open, has a OpenCV library in it. If i double Click the .jar, then it work fine. If I Type it in the Mac OS X terminal it works fine aswell.

@Override
public void actionPerformed(ActionEvent e) {
    if(e.getSource() instanceof JButton){
        JButton button = (JButton) e.getSource();
        if(button.getText().equals("Aktivieren")){

            System.out.println("Yaay");
                String str;

                if(Main.os.contains("Mac")){
                    str = "java -jar "+System.getProperty("user.home") + "/Library/Application\\ Support/MW"+"/System/Core.jar";
                    System.out.println(str);
                } else {
                    str = "java -jar "+Main.directory+"/MW/System/Core.jar";
                }

                System.out.println(str);

                try {
                    Runtime.getRuntime().exec(str);
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }

        }
    }
}

So the output of the Sysout is: java -jar /Users/Tech/Library/Application\ Support/MW/System/Core.jar

And when i paste it manually in the Terminal it works just fine.

And I get no Error Messages.

Sorry for my Bad English, im german.

Thanks (or whatever) Tech

Tech
  • 21
  • 4
  • If you need it for a project and use an IDE like Eclipse, you need to add `a jar to the buildpath`. There are tons of resources about this and I don't want to post a fresh one to avoid duplication. – Debosmit Ray Mar 21 '16 at 22:15
  • I think this could be because of classpath issue. – Raghu K Nair Mar 21 '16 at 22:18
  • This fails because you can not use `Runtime.exec(String)` with any parameters that contain spaces or backslashes. Use `Runtime.exec(String[])` instead, e.g. `.exec(new String[] { "java", "-jar", System.getProperty("user.home") + "/Library/Application Support/MW/System/Core.jar") })` (note no backslashes) – that other guy Mar 21 '16 at 22:20
  • Its working! Thank you all <3 For others with the same problem, try this out. – Tech Mar 22 '16 at 10:10

1 Answers1

0

Could you try doing this?

if(Main.os.contains("Mac")){
    String jar = "System.getProperty("user.home") + "/Library/Application\\ Support/MW"+"/System/Core.jar";
    str = "java -classpath " + jar + " -jar "+System.getProperty("user.home") + "/Library/Application\\ Support/MW"+"/System/Core.jar";
    System.out.println(str);
} else {
    String jar = "Main.directory+"/MW/System/Core.jar";
    str = "java  -classpath " + jar + " -jar "+Main.directory+"/MW/System/Core.jar";
}

In general I would love to use ProcessBuilder but that will not solve your problem :)

Debosmit Ray
  • 5,228
  • 2
  • 27
  • 43
Raghu K Nair
  • 3,854
  • 1
  • 28
  • 45