2014-02-08 69 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,你是對的..謝謝 –

相關問題