问题描述
我遇到的问题是当我使用 cd
命令,路径被复位。例如,如果我做 cd bin
,然后 cd ../
,我将从目录执行第一个
如果我想要访问一个特定的文件夹并执行一个程序,我必须这样做: / p>
cd C:\mydir& cd bin& start.exe
我想要做的是在不同的部分拆分此cmd:
cd C:\ mydir
然后 cd bin
/ code>
我该如何做?是否有一种方法来存储当前的 cd
路径并使用它?
这里是我使用的代码:
String [] cmd_exec = new String [] {cmd / c,cmd};
Process child = Runtime.getRuntime()。exec(cmd_exec);
BufferedReader in = new BufferedReader(new InputStreamReader(child.getInputStream()));
StringBuffer buffer = new StringBuffer();
buffer.append(>+ cmd +\\\
);
字符串;
while((line = in.readLine())!= null)
{
buffer.append(line +\\\
);
}
in.close();
child.destroy();
return buffer.toString();
它执行命令,然后返回控制台的内容。
感谢,我能够做到:
这里是我使用的代码:
if(cmd.indexOf(cd)> = 0)
{
String req_cmd = cmd.substring(0, 3);
String req_path = cmd.substring(3);
if(req_path.startsWith(File.separator)|| req_path.substring(1,2).equals(:))
path = req_path;
else
if(new File(path + cmd.substring(3))。exists())
path + = cmd.substring(3);
else return[错误]目录不存在。
if(!path.endsWith(File.separator))path + = File.separator;
cmd = req_cmd + path;
}
else cmd =cd+ path +&+ cmd;然后你可以执行命令:
Runtime.getRuntime()。exec(new String [] {cmd,/ c,cmd});
不要忘记在类中添加此属性:
private static String path = System.getProperty(user.dir)+ File.separator;
I want to create a full cross-plateform console in Java.
The problem I have is when I use the cd
command, the path is reset. For example, if I do cd bin
, then cd ../
, I will execute the first one from the directory of my app and the second one exactly from the same directory.
If I want to go to a specific folder and execute a program I have to do something like that:
cd C:\mydir & cd bin & start.exe
What I want to do is to split this cmd in different parts:
cd C:\mydir
then cd bin
then start.exe
How can I do that? Is there a way to store the current cd
path and use it then?
Here is the code I use:
String[] cmd_exec = new String[] {"cmd", "/c", cmd};
Process child = Runtime.getRuntime().exec(cmd_exec);
BufferedReader in = new BufferedReader(new InputStreamReader(child.getInputStream()));
StringBuffer buffer = new StringBuffer();
buffer.append("> " + cmd + "\n");
String line;
while ((line = in.readLine()) != null)
{
buffer.append(line + "\n");
}
in.close();
child.destroy();
return buffer.toString();
It executes the command, then return the content of the console. (This is for windows for the moment).
Thanks to Mads, I was able to do the trick:
Here is the code I used:
if (cmd.indexOf("cd ") >= 0)
{
String req_cmd = cmd.substring(0, 3);
String req_path = cmd.substring(3);
if (req_path.startsWith(File.separator) || req_path.substring(1, 2).equals(":"))
path = req_path;
else
if (new File(path + cmd.substring(3)).exists())
path += cmd.substring(3);
else return "[Error] Directory doesn't exist.\n";
if (!path.endsWith(File.separator)) path += File.separator;
cmd = req_cmd + path;
}
else cmd = "cd " + path + " & " + cmd;
Then you can execute the command calling:
Runtime.getRuntime().exec(new String[] {"cmd", "/c", cmd});
Don't forget to add this attribute in your class:
private static String path = System.getProperty("user.dir") + File.separator;
这篇关于Java - Exec控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!