0

I have a very large/old/long running project that accesses file resources using paths that are relative to the launch directory (i.e. 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

Community
  • 1
  • 1
John
  • 3,458
  • 4
  • 33
  • 54
  • 1
    Discussed (at length) also here http://stackoverflow.com/questions/840190/changing-the-current-working-directory-in-java – dhke Aug 31 '15 at 13:16
  • ...except that (as I'm reading it), the other post indicates that this is not (reliably) possible. Since Eclipse does have the ability to set this in run configurations it must be somehow possible. – John Aug 31 '15 at 15:18
  • 1
    Eclipse forks off the debugged application in a new process. And [`Runtime.exec()`](https://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#exec%28java.lang.String[],%20java.lang.String[],%20java.io.File%29) does take an (optional) working directory. Which is mentioned in [one of the answers](https://stackoverflow.com/questions/840190/changing-the-current-working-directory-in-java#answer-14732499). – dhke Aug 31 '15 at 15:23

2 Answers2

1

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!

Community
  • 1
  • 1
vathek
  • 531
  • 2
  • 9
0

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;
}
John
  • 3,458
  • 4
  • 33
  • 54