我有一个Java程序,它在运行时将一些可执行文件提取到特定文件夹,并尝试运行它们。当然,在运行可执行文件之前,需要更改其权限。为此,我使用以下代码:
public static void changePermissions(String filename,String path){
String[] cmd=new String[3];
cmd[0]="chmod";
cmd[1]="u+x";
cmd[2]=filename;
BetterRunProcess process=new BetterRunProcess();
process.runProcessBuilderInDifferentDirectory(cmd,path,1,0,0,"");
}
在上面的代码片段中,变量path包含可执行文件的路径,filename是可执行文件的名称。这一行:
process.runProcessBuilderInDifferentDirectory(cmd,path,1,0,0,"");
执行命令“chmod u + x ...”。在我自己的计算机上,代码工作正常,但是当我在别人的计算机上运行时,会抛出以下错误:
chmod: changing permissions of deviceQuery.out. Operation not permitted.
有人可以弄清楚这背后可能出现的问题吗?
以下是一些可能有用的代码。
public void runProcessBuilderInDifferentDirectory(String[] cmd,String path,int printToConsole,int printToExternalFile,int append,String fileName){
ProcessBuilder builder;
if(cmd.length==1) builder=new ProcessBuilder(cmd[0]);
else if(cmd.length==2) builder=new ProcessBuilder(cmd[0],cmd[1]);
else if(cmd.length==3) builder=new ProcessBuilder(cmd[0],cmd[1],cmd[2]);
else if(cmd.length==4) builder=new ProcessBuilder(cmd[0],cmd[1],cmd[2],cmd[3]);
else builder=new ProcessBuilder(cmd[0],cmd[1],cmd[2],cmd[3],cmd[4]);
builder.directory(new File(path));
try {
Process pr=builder.start();
if(printToConsole==1) printToConsole(pr);
if(printToExternalFile==1) printToExternalFile(pr,fileName,append);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
谢谢!
答案 0 :(得分:0)
从具有该文件权限的用户运行您的Java代码。