简体   繁体   English

Runtime.getruntime()。exec()在Linux上不起作用

[英]Runtime.getruntime().exec() doesn't work on Linux

I have a Java Runtime.getRuntime().exec() problem. 我有一个Java Runtime.getRuntime().exec()问题。 I run my java app on Linux and just need to execute a basic task: copy and rename a file using cp command. 我在Linux上运行我的Java应用程序,只需要执行一个基本任务:使用cp命令复制并重命名文件。 However, it doesn't seem to work. 但是,它似乎不起作用。

This is my chunk of code to call the Runtime.getRuntime().exec() : 这是我调用Runtime.getRuntime().exec()的代码块:

String command = "cp -f '" + old_path + "' " + song_info[6] + ".mp3";                
System.out.println(command);
log.info(command);
            try{
                p = Runtime.getRuntime().exec(command);
                int returnCode;
                try {
                    returnCode = p.waitFor();
                    System.out.println("Return code = " + returnCode);
                } catch (InterruptedException ex) {
                    java.util.logging.Logger.getLogger(Import.class.getName()).log(Level.SEVERE, null, ex);
                }

            } catch(IOException e){log.error(e);}

When I run the java app the command for each loop is something like this 当我运行Java应用程序时,每个循环的命令都是这样的

cp -f '/temp_storage/LoveSongs/28.I miss you.mp3' /music_storage/data/0/0/1/108.mp3

If I copy that log line and run it in the command line, it works perfectly. 如果我复制该日志行并在命令行中运行它,则可以正常运行。 However the result from java app always return code 1 (which indicate failure). 但是,Java应用程序的结果始终返回代码1(表示失败)。 Even including /bin/bash -c before the command string, it still doesn't work. 即使在命令字符串之前包含/bin/bash -c ,它仍然不起作用。

Any suggestion why? 有什么建议吗? I've just install JRE and JDK on that server. 我刚刚在该服务器上安装了JRE和JDK。 When I type java -version I got: 当我输入java -version我得到了:

java version "1.6.0_17" OpenJDK Runtime Environment (IcedTea6 1.7.10) (rhel-1.20.b17.el5-x86_64) OpenJDK 64-Bit Server VM (build 14.0-b16, mixed mode) Java版本“ 1.6.0_17” OpenJDK运行时环境(IcedTea6 1.7.10)(rhel-1.20.b17.el5-x86_64)OpenJDK 64位服务器VM(内部版本14.0-b16,混合模式)

AFAIK, if you command has multiple command line parameters you need to supply them in a String[], have you tried that? AFAIK,如果您的命令具有多个命令行参数,则需要在String []中提供它们,您是否尝试过? Furthermore if you need to copy files around you can use FileUtils or Guava libraries (dont remember what it's called there) 此外,如果您需要在周围复制文件,则可以使用FileUtils或Guava库(不要记住那里的名称)

see this relevant thread about copying files 看到有关复制文件的相关主题

Does the user that the application is running under have the necessary permissions? 运行应用程序的用户是否具有必需的权限? And from the process, have you tried getting hold of stderr to see if there is anything useful in there? 从这个过程中,您是否尝试过掌握stderr以查看其中是否有任何有用的东西?

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

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