Naren
Naren

Reputation: 3

What is the right way to execute a command in java

I need to execute a command in command prompt using java. The command works correctly when i type it in the prompt and tutorial.mallet file is created accordingly. But when i do it through the code, nothing is happening.

The command is:

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

And this is my code

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();
}

Upvotes: 0

Views: 145

Answers (2)

Ferrakkem Bhuiyan
Ferrakkem Bhuiyan

Reputation: 2783

Right way to execute a command in 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

Upvotes: 0

BackSlash
BackSlash

Reputation: 22243

You can't change the working directory like this, but you can specify it as parameter to the exec method:

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

Upvotes: 2

Related Questions