2014-02-08 161 views
0

我需要在命令提示符下使用java执行命令。当我在提示符中键入命令并且tutorial.mallet文件相应地创建时,该命令正常工作。但是当我通过代码完成时,没有任何事情发生。在java中执行命令的正确方法是什么

的命令是:

C:\mallet> bin\mallet import-dir --input E:\InputFilesForTopicModeling --output E:\Tutorial\tutorial.mallet --keep-sequence --remove-stopwords 

这是我的代码

try { 
    Runtime rt=Runtime.getRuntime(); 
    rt.exec("cmd /c"+ "cd mallet"); 
    String export=" bin\\mallet import-dir --input E:\\InputFilesForTopicModeling --output E:\\Tutorial\tutorial.mallet --keep-sequence --remove-stopwords"; 
    rt.exec("cmd /c"+export); 
} catch(Exception e) { 
    e.printStackTrace(); 
} 
+0

提示:缩进代码。 – 2014-02-08 10:46:07

回答

2

你不能改变这样的工作目录,但您可以指定它作为参数传递给exec方法:

rt.exec("bin/mallet import-dir --input E:/InputFilesForTopicModeling --output E:/Tutorial/tutorial.mallet --keep-sequence --remove-stopwords", 
    null, new File("C:/mallet")); 
+0

谢谢,但是当我运行这个声明时,我得到一个异常,指出“无法运行程序bin \ mallet”(在目录“C:\ Mallet”中):CreateProcess error = 2,系统找不到指定的文件“ – Naren

+0

Your command指向一个路径为“C:/ mallet/bin/mallet”的可执行文件,你确定它存在吗? – BackSlash

+0

Ya ..“bin \ mallet import-dir --input E:\ InputFilesForTopicModeling --output E:\ Tutorial \ tutorial.mallet --keep-sequence --remove-stopwords”我在命令提示符中执行了这条语句,它是工作正常并且tutorial.mallet文件也被创建 – Naren

0

在Java中执行命令的正确方法。

1. Cd YourDirectory //Go to your directory where you put your Java 
    code. Example: cd F: 

2. Cd yourJavaProject//Go to your directory where you put your Java 
    project. Example :cd JavaProject 

3. javac posMain.java //Compile the Java file 

4. java posMain //Don't use .java after that you will get your program 
    output 
+0

我认为你误解了这个问题 – BackSlash

+0

可能@Tiny你是对的我认为“我需要在命令提示符下使用java执行命令”,这是一个值得关注的问题。对不起, –

+0

是@Tiny,你是对的..谢谢 –

相关问题