从Java内部运行命令行操作 [英] Running a command-line operation from within Java

查看:88
本文介绍了从Java内部运行命令行操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我构建了一个非常简单的程序来测试运行独立于Java的命令行操作。也就是说:以后,我希望能够修改此代码,从使用移动到我可以在命令行中输入的任何其他命令(包括调用其他非Java软件)。



我确实搜索并阅读了大概两个答案,但是它们要么暗示我尝试正确,要么对我的简单测试不感兴趣,或者提出了其他无效的解决方案(例如使用.exec(String [ ])方法而不是.exec(String)-结果相同!)。



这是我的代码:

  import java.io.IOException; 

公共类RunCommand {

private静态最终字符串PATH_OUT = C:\\Users\\me\\Desktop\\Temp\ \\out\\;
private static final String FILE = sample.txt;
private static final String PATH_IN = C:\\Users\\me\\Desktop\\Temp\\in\\;

public static void main(String [] args){
try {
String command = move + PATH_IN + FILE + + PATH_OUT;
System.out.println( Command: + command);
Runtime.getRuntime()。exec(command);
} catch(IOException e){
e.printStackTrace();
}
}
}

这是我看到的输出当我运行时:

 命令:move C:\Users\myingling\Desktop\CDS\Temp\ in\sample.txt C:\\ Users \\ myingling \\ Desktop\CDS\Temp\out\ 
java.io.IOException:无法运行程序 move:CreateProcess error = 2,系统找不到在java.lang.ProcessBuilder.start(未知源)处指定的
文件在java.lang.Runtime.exec(java.lang.Runtime)处指定的文件
exec(未知源)
在java.lang.Runtime.exec(未知源)
在RunCommand.main(RunCommand.java:13)
原因:java.io.IOException:CreateProcess错误= 2,系统无法在java.lang.ProcessImpl.create(本机方法)
找到指定的文件
。< init>(未知源)
at java.lang.ProcessImpl.start(未知源)
... 5个以上

请注意,当我将命令复制/粘贴到命令提示符窗口中时,文件将移动



我缺少什么?我读过的所有其他问题似乎都表明这应该可行。



谢谢!



编辑,现在可以使用,谢谢大家的帮助!令人讨厌的是,它是cmd.exe参数的移动方式的隐藏方式。我希望他们做到了,如果它在复制/粘贴时起作用,在您调用.exec()方法时起作用。哦,好吧。

解决方案

move命令是cmd.exe解释器的一部分,本身不是可执行文件。 / p>

这将起作用:

  cmd.exe / c移动file1 file2 


I built a very simple program to test running a command line operation separate of Java. That is: later I want to be able to modify this code from using "move" to any other command I can enter into the command line (including calling other, non-Java, software).

I did search and read probably two dozen answers, but they all either suggested I was trying this correctly, were irrelevent to my simple test or proposed other solutions which did not work (like using the .exec(String[]) method instead of .exec(String) - same result!).

Here is my code:

import java.io.IOException;

public class RunCommand {

private static final String PATH_OUT = "C:\\Users\\me\\Desktop\\Temp\\out\\";
private static final String FILE = "sample.txt";
private static final String PATH_IN = "C:\\Users\\me\\Desktop\\Temp\\in\\";

public static void main(String[] args) {
    try {
        String command = "move "+PATH_IN+FILE+" "+PATH_OUT;
        System.out.println("Command: "+command);
        Runtime.getRuntime().exec(command);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}

Here is what I see output when I run:

Command: move C:\Users\myingling\Desktop\CDS\Temp\in\sample.txt C:\Users\myingling\Desktop\CDS\Temp\out\
java.io.IOException: Cannot run program "move": CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at RunCommand.main(RunCommand.java:13)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(Unknown Source)
at java.lang.ProcessImpl.start(Unknown Source)
... 5 more

Note that when I copy/paste the command into a command prompt window, the file moves successfully though.

What am I missing? All the other questions I've read seem to indicate this should work.

Thanks!

EDIT Works now, thanks for the help everyone! It's annoying that it's hidden the way "move" is a parameter of cmd.exe. I wish they had made it so if it worked when copy/pasted it worked when you called the .exec() method. Oh well.

解决方案

The "move" command is part of the cmd.exe interpreter, and not a executable by itself.

This would work:

cmd.exe /c move file1 file2 

这篇关于从Java内部运行命令行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆