如何从另一个JAR启动带有运行时命令的JAR?

时间:2014-04-03 07:39:08

标签: java runtime.exec

这是我使用的代码:

public class CLASSNAME extends JFrame
{

public static String MYAPP = new String(Util.getWorkingDirectory() + File.separator + "MYAPP.jar");
public static File MYJAR = new File(MYAPP); //used in other sections of the code

然后当我调用运行时命令它不会启动时,它适用于Linux但不适用于Windows(也可能适用于MAC):

public static final void launching() throws Exception {
      try {

        Runtime.getRuntime().exec (MYAPP);
        System.exit(0);
        } catch (IOException e1) {
            e1.printStackTrace();
        } catch (Exception e1) {
            e1.printStackTrace();
        }
  }

编辑:那是我如何使代码工作(希望对其他人有帮助):

public class CLASSNAME extends JFrame
{

public static String MYAPP = new String(Util.getWorkingDirectory() + File.separator + "MYAPP.jar");

//THE JFRAME CODE AND THEN:

public static final void launching() throws Exception {
try {
String osName = System.getProperty("os.name");

        if(osName.startsWith("Win")) {
            Runtime.getRuntime().exec (new String[]{"javaw","-jar",MYAPP});
            System.exit(0);
        } 
        if(osName.startsWith("Linux")) {
            Runtime.getRuntime().exec (new String[]{"java","-jar",MYAPP});
            System.exit(0);
        }
        if(osName.startsWith("Mac")) {
            Runtime.getRuntime().exec (new String[]{"java","-jar",MYAPP});
            System.exit(0);;
        }


        }catch (IOException e1) {
            e1.printStackTrace();
        } catch (Exception e1) {
            e1.printStackTrace();
        }
  }
以这种方式在linux上运行MYAPP,即使没有标记为可运行位

1 个答案:

答案 0 :(得分:-1)

Runtime.getRuntime().exec(command)

如果你可以在终端或cmd中运行命令,那就是正确的。