如何在 Java 中更改目录并执行其中的文件
How to change directory and execute a file there in Java
我必须在 Java 中执行 Windows 的以下 cmd
命令。
cmd
命令:
Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
//First i have to change my default directory//
C:\Users\shubh>D:
//Then move to a specific folder in the D drive.//
D:\>cd MapForceServer2017\bin\
//then execute the .mfx file from there.
D:\MapForceServer2017\bin>mapforceserver run C:\Users\shubh\Desktop\test1.mfx
执行结果
Output files:
library: C:\Users\shubh\Documents\Altova\MapForce2017\MapForceExamples\Tutorial\library.xml
Execution successful.
我建议使用 https://commons.apache.org/proper/commons-exec/ 从 Java 中执行 o/s 命令,因为它处理了您以后可能遇到的各种问题。
您可以使用:
CommandLine cmdLine = CommandLine.parse("cmd /c d: && cd MapForceServer2017\bin\ && mapforceserver run ...");
DefaultExecutor executor = new DefaultExecutor();
int exitValue = executor.execute(cmdLine);
前段时间我有一些相同的要求,当时我认为我从堆栈中得到了以下片段。试试这个。
String[] command =
{
"cmd",
};
Process p = Runtime.getRuntime().exec(command);
new Thread(new SyncPipe(p.getErrorStream(), System.err)).start();
new Thread(new SyncPipe(p.getInputStream(), System.out)).start();
PrintWriter stdin = new PrintWriter(p.getOutputStream());
stdin.println("dir c:\ /A /Q");
// write any other commands you want here
stdin.close();
int returnCode = p.waitFor();
System.out.println("Return code = " + returnCode);
同步管道Class:
class SyncPipe implements Runnable
{
public SyncPipe(InputStream istrm, OutputStream ostrm) {
istrm_ = istrm;
ostrm_ = ostrm;
}
public void run() {
try
{
final byte[] buffer = new byte[1024];
for (int length = 0; (length = istrm_.read(buffer)) != -1; )
{
ostrm_.write(buffer, 0, length);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
private final OutputStream ostrm_;
private final InputStream istrm_;
}
我必须在 Java 中执行 Windows 的以下 cmd
命令。
cmd
命令:
Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
//First i have to change my default directory//
C:\Users\shubh>D:
//Then move to a specific folder in the D drive.//
D:\>cd MapForceServer2017\bin\
//then execute the .mfx file from there.
D:\MapForceServer2017\bin>mapforceserver run C:\Users\shubh\Desktop\test1.mfx
执行结果
Output files:
library: C:\Users\shubh\Documents\Altova\MapForce2017\MapForceExamples\Tutorial\library.xml
Execution successful.
我建议使用 https://commons.apache.org/proper/commons-exec/ 从 Java 中执行 o/s 命令,因为它处理了您以后可能遇到的各种问题。
您可以使用:
CommandLine cmdLine = CommandLine.parse("cmd /c d: && cd MapForceServer2017\bin\ && mapforceserver run ...");
DefaultExecutor executor = new DefaultExecutor();
int exitValue = executor.execute(cmdLine);
前段时间我有一些相同的要求,当时我认为我从堆栈中得到了以下片段。试试这个。
String[] command =
{
"cmd",
};
Process p = Runtime.getRuntime().exec(command);
new Thread(new SyncPipe(p.getErrorStream(), System.err)).start();
new Thread(new SyncPipe(p.getInputStream(), System.out)).start();
PrintWriter stdin = new PrintWriter(p.getOutputStream());
stdin.println("dir c:\ /A /Q");
// write any other commands you want here
stdin.close();
int returnCode = p.waitFor();
System.out.println("Return code = " + returnCode);
同步管道Class:
class SyncPipe implements Runnable
{
public SyncPipe(InputStream istrm, OutputStream ostrm) {
istrm_ = istrm;
ostrm_ = ostrm;
}
public void run() {
try
{
final byte[] buffer = new byte[1024];
for (int length = 0; (length = istrm_.read(buffer)) != -1; )
{
ostrm_.write(buffer, 0, length);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
private final OutputStream ostrm_;
private final InputStream istrm_;
}