在java中执行命令的正确方法是什么
我需要使用java在命令提示符中执行命令。当我在提示符中键入该命令并相应地创建tutorial.mallet文件时,该命令工作正常。但是,当我通过代码执行时,什么都没有发生 命令是:在java中执行命令的正确方法是什么,java,Java,我需要使用java在命令提示符中执行命令。当我在提示符中键入该命令并相应地创建tutorial.mallet文件时,该命令工作正常。但是,当我通过代码执行时,什么都没有发生 命令是: C:\mallet> bin\mallet import-dir --input E:\InputFilesForTopicModeling --output E:\Tutorial\tutorial.mallet --keep-sequence --remove-stopwords 这是我的密码 try
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();
}
您不能像这样更改工作目录,但可以将其指定为
exec
方法的参数:
rt.exec("bin/mallet import-dir --input E:/InputFilesForTopicModeling --output E:/Tutorial/tutorial.mallet --keep-sequence --remove-stopwords",
null, new File("C:/mallet"));
在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
提示:缩进你的代码。我想你误解了这个问题可能是@Tiny你是对的,我认为“我需要使用java在命令提示符下执行命令”这是一个值得关注的问题。对此我很抱歉不,我没有评论。我刚刚编辑了这篇文章以改进代码格式:)谢谢,但当我运行这条语句时,我得到一个异常,说明“无法运行程序”bin\mallet(在目录“C:\mallet”):CreateProcess error=2,系统找不到指定的文件“您的命令指向路径为
C:/mallet/bin/mallet
的可执行文件,你确定它存在吗?是的。。“bin\mallet import dir--input E:\InputFilesForTopicModeling--output E:\Tutorial\Tutorial.mallet--keep sequence--remove stopwords”我在命令提示符下执行了这条语句,它工作正常,Tutorial.mallet文件也是用rt.exec创建的(“C:/mallet/bin/malletimport dir--input E:/InputFilesForTopicModeling--output E:/Tutorial/Tutorial.mallet--keep sequence--remove stopwords”);
非常感谢您…做对了rt.exec(“C:/mallet/bin/malletimport dir--input E:/InputFilesForTopicModeling--output E:/Tutorial/Tutorial.mallet--remove--remove stopwords--remove,空,新文件(“C:\\Mallet\\”);