0

I am trying to execute cmd commands inside a java program using the following code

            String command = "clingo F:\\clingo\\food1.lp F:\\clingo\\fooddata.txt"
                + " 0"+" >>F:\\clingo\\foodout.txt";
        Process p1 = Runtime.getRuntime().exec(command);

This is executing in java without any exceptions, but the actual command is not running. If the command is run it should create text file foodout.txt in the location mentioned. Nothing is happening.

The actual command is

clingo food1.lp fooddata.txt 0 >>foodout.txt

clingo is a windows executable program. This command works fine when run in command prompt. I want to run this inside java program from click of a button. I have set environment variable for clingo. Clingo and this java project are in the same directory.

Before this i tried below code

        String[] command = {"clingo", "food1.lp","fooddata.txt", "0", ">>foodout.txt"};
        ProcessBuilder builder = new ProcessBuilder(command);
        builder.directory(new File(WorkingDirectoryArea.getText()));
        Process process = builder.start();

where Workingdirectoryarea contains the directory location for commands to be run. This code does nothing.

Can someone guide me or provide code sample on how to run the cmd command inside this java program. I am using Netbeans IDE. Thanks.

3 Answers 3

0

you said your command works with a command prompt. OK. If you look closely, the command window has a path entry (cmd= echo %PATH%). That's the difference between executing a command in a command window and executing a java process. You have 2 options.

1. Add the path to the process.
2. Add the path to the clingo command (i.e. "f:\path\clingo.exe ...)

Item 1 is especially needed when using dos commands. To add a path environment: Runtime.getRuntime().exec not finding file in java environment

Sign up to request clarification or add additional context in comments.

1 Comment

OK. 3. I forgot about "cmd /c like @whbogado said.
0

You are redirecting standard output to a file. This is not part of the command nor a command line parameter. Is the command interpreter that handles this.

You must invoke the command interpreter to run your program like this:

String command = "cmd /c clingo F:\\clingo\\food1.lp F:\\clingo\\fooddata.txt"
            + " 0"+" >>F:\\clingo\\foodout.txt";
Process p1 = Runtime.getRuntime().exec(command);

Note the cmd /cpart which invokes the command interpreter to run your command like you would do on a Windows terminal.

On Linux it would be sh -c or whatever shell you like.

EDIT 1

When running the command, clingo.exe must be in your path or it must be in the default directory for the Java interpreter. If not, you should give the full path to the executable, like this:

String command = "cmd /c F:\\clingo\\clingo F:\\clingo\\food1.lp F:\\clingo\\fooddata.txt"
            + " 0"+" >>F:\\clingo\\foodout.txt";

Try to run

F:\\clingo\\clingo F:\\clingo\\food1.lp F:\\clingo\\fooddata.txt 0  >> F:\\clingo\\foodout.txt

at a Windows prompt and see if it works as expected. If it works it also should work when run from a Java program. Please, replace the clingo path with the right one for your environment.

Comments

-1

Your command must be like this: java -jar yourExecuteable.jar yourParameter In your case: java -jar clingo.jar food1.lp fooddata.txt 0 >>foodout.txt

1 Comment

There isn't a clingo.jar to run. Your answer is unrelated to the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.