简体   繁体   English

从命令行运行jar文件时设置目录路径

[英]Setting directory path while running jar file from command line

I have a jar file, in which I have function which detects its absolute path (current directory from where this file is launched). 我有一个jar文件,其中我有一个函数,它检测其绝对路径(此文件的启动位置)。 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: 但现在,为了获得管理员权限,我从另一个jar文件启动此文件,同时使用高程二进制可执行文件,如:

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. 在这里,请注意Elevate.exe和jar文件在同一个目录中,但我尝试同时给出jar文件名和绝对路径,但在两种方式中,文件启动,它有管理员权限,一切正常除了事实,现在相同的方法给出了错误的当前目录路径。

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 "); 这次我得到: C:\\Windows\\System32我尝试过很多东西,但是每当我使用Runtime.getRuntime().exec("cmd \\\\c ");从另一个jar文件中使用上面的方法启动这个jar时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. 独立启动文件时返回相同的正确路径,从外部进程启动时返回C:\\Windows\\System32 Please suggest what should I do to solve this problem. 请建议我该怎么做才能解决这个问题。

Let's have a look the exec method of class Runtime: 我们来看看Runtime类的exec方法:

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). 调用exec(command)形式的行为与调用exec(command,null,null)完全相同。

and then the description of exec(command, null, null): 然后是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) 调用exec(command,envp,dir)的形式与调用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): 所以exec(命令)的调用会导致调用exec(cmdarray,envp,dir),下面是exec(cmdarray,envp,dir)描述的一部分:

The working directory of the new subprocess is specified by dir. 新子进程的工作目录由dir指定。 If dir is null, the subprocess inherits the current working directory of the current process. 如果dir为null,则子进程将继承当前进程的当前工作目录。

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 在您的代码中,参数dir为null,因此子进程继承当前进程的当前工作目录,在这种情况下,是Evalute.exe的工作目录

C:\\Windows\\System32 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: 如果你想得到与jar文件执行方式无关的相同结果,你必须明确指定工作目录,只需使用下面的代码:

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

instead of: 代替:

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

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

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