简体   繁体   English

从指定目录启动Java应用程序(主)

[英]Launch Java application (main) from specified directory

I have a very large/old/long running project that accesses file resources using paths that are relative to the launch directory (ie the application will only work if launched from a specific directory). 我有一个非常大/老/长期运行的项目,该项目使用相对于启动目录的路径访问文件资源(即,该应用程序仅在从特定目录启动时才起作用)。 When I need to debug the program I can launch it from eclipse and set the start directory using Run Configurations->->Working directory. 当我需要调试程序时,可以从Eclipse中启动它,并使用“运行配置”->->“工作目录”来设置开始目录。 I would like to be able to write a single Java class that will launch the main class from a specified directory. 我希望能够编写一个Java类,该类将从指定目录启动主类。 Is this possible and if it is how would I do it? 这可能吗,如果我会怎么做? I have found several related items including those shown below but can't seem to find the answer I'm looking for. 我发现了一些相关的项目,包括以下所示的项目,但似乎找不到我要的答案。

https://community.oracle.com/thread/1257595?start=0&tstart=0 https://community.oracle.com/thread/1257595?start=0&tstart=0

http://www.javapractices.com/topic/TopicAction.do?Id=243 http://www.javapractices.com/topic/TopicAction.do?Id=243

How do I run a java program from a different directory? 如何从其他目录运行Java程序?

Java - start another class' main in a different process Java-在不同的过程中启动另一个类的main

According to this you can write a simple class with main that: 根据这个你可以写与主要的是一个简单的类:

  1. ask for a working directory 要求一个工作目录
  2. copy your jar in the choosen directory 将您的jar复制到所选目录中
  3. execute the jar 执行罐子
  4. delete the jar from the choose directory. 从选择目录中删除jar。

Ex. 例如

public class Exec
{
   public static void main(String []args) throws Exception
    {   choosenDir=askForWorkingDirectory()
        jarFileNameWithabsolutePath=copyJarIntoDir(choosenDir)
        Process ps=Runtime.getRuntime().exec(new String[]{"java","-jar",jarFileNameWithabsolutePath});
        ps.waitFor();
        java.io.InputStream is=ps.getInputStream();
        byte b[]=new byte[is.available()];
        is.read(b,0,b.length);
        System.out.println(new String(b));
        deleteJarFormChoosenDir(jarFileNameWithabsolutePath);
    }
}

Where: 哪里:

askForWorkingDirectory() show a DirectoryChooser dialog and return the absolute path. askForWorkingDirectory()显示DirectoryChooser对话框并返回绝对路径。

copyJarIntoDir(choosenDir) receive the choosen directory where copy the jar file and returns the absolute path of the jar file with file name. copyJarIntoDir(choosenDir)接收选择的目录,在该目录中复制jar文件,并返回具有文件名的jar文件的绝对路径。

deleteJarFormChoosenDir(jarFileNameWithabsolutePath) finally remove the copied jar deleteJarFormChoosenDir(jarFileNameWithabsolutePath)最终删除复制的jar

Hope I helped you! 希望我能帮到您!

Using Runtime.getRuntime.exec with the optional runtime directory paramerter worked for me. 将Runtime.getRuntime.exec与可选的运行时目录参数配合使用对我来说很有效。

This is what I used: 这是我使用的:

public static void main(String[] args) throws Exception {
    String classPath = getClassPath();
    Runtime.getRuntime().exec("java -cp " + classPath + " com.mycompany.MyApp", null, MY_WORKING_DIR);
}

private static String getClassPath() {
    StringBuffer buffer = new StringBuffer();
    URLClassLoader urlClassLoader = ((URLClassLoader) (Thread.currentThread().getContextClassLoader()));
    URL[] urls = urlClassLoader.getURLs();
    for (URL url : urls) {
        buffer.append(new File(url.getPath()));
        buffer.append(System.getProperty("path.separator"));
    }
    String rtn = buffer.toString();
    return rtn;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM