简体   繁体   中英

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. I would like to be able to write a single Java class that will launch the main class from a specified directory. 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

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

How do I run a java program from a different directory?

Java - start another class' main in a different process

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
  3. execute the jar
  4. delete the jar from the choose directory.

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.

copyJarIntoDir(choosenDir) receive the choosen directory where copy the jar file and returns the absolute path of the jar file with file name.

deleteJarFormChoosenDir(jarFileNameWithabsolutePath) finally remove the copied jar

Hope I helped you!

Using Runtime.getRuntime.exec with the optional runtime directory paramerter worked for me.

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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