0
votes

I have a jar file, in which I have function which detects its absolute path (current directory from where this file is launched). It works fine.

But now, in order to get admin rights, I launch this file from another jar file, while using elevation binary executable something like:

Elevate.exe javaw.exe -jar path-to-my-jar-file  

Here, please note that Elevate.exe and jar file are in the same directory, however I tried giving both just the jar file name as well as absolute path, but in both ways, the file launches, it has admin rights and everything works fine except the fact that, now the same method gives a wrong current directory path.

This time I get: C:\Windows\System32 I have tried many things, but every time when I launch this jar using above method from another jar file using Runtime.getRuntime().exec("cmd \\c "); the current directory path is not detected correctly. Why is that so, and how can I find out correct path in such a case?

EDIT: I tried getting a correct directory name using:

String dir = System.getProperty("user.dir") &&
String dir = new File (".").getAbsolutePath().toString();

Both return the same correct path when file is launched independently, and C:\Windows\System32 when launched from external process. Please suggest what should I do to solve this problem.

1

1 Answers

2
votes

Let's have a look the exec method of class Runtime:

This is a convenience method. An invocation of the form exec(command) behaves in exactly the same way as the invocation exec(command, null, null).

and then the description of exec(command, null, null):

This is a convenience method. An invocation of the form exec(command, envp, dir) behaves in exactly the same way as the invocation exec(cmdarray, envp, dir)

so a invocation of exec(command) results in a invocation of exec(cmdarray, envp, dir), below is part of the description of exec(cmdarray, envp, dir):

The working directory of the new subprocess is specified by dir. If dir is null, the subprocess inherits the current working directory of the current process.

In your code the parameter dir is null, so the subprocess inherits the current working directory of the current process, in this case, is working directory of Evalute.exe

C:\Windows\System32

.

if your want to get the same result regardingless of how the jar file is executed, you must specify the working directory explicitly, just use the code below:

Runtime.getRuntime().exec("cmd \\c ", null,  new File("your working dir"));

instead of:

Runtime.getRuntime().exec("cmd \\c ");