Execute External Program Through Terminal in Java

Execute external program through terminal in Java

This is normal: you are attempting to launch a command normally issued by a shell.

Here, <proof.in and >proof.out are taken as literal arguments to the otter executable, and not shell redirections. But seeing the home page for this tool, it will not work: it expects data on stdin, which the redirect normally provides.

You need to launch this command via a shell, and preferably using a process builder:

final ProcessBuilder pb = new ProcessBuilder("/bin/sh", "-c", "otter <proof.in >proof.out");
final Process p = pb.start();

etc etc.

You should also ensure, of course, that the program runs from the correct directory -- fortunately, ProcessBuilder also allows you to do that.

Execute external program

borrowed this shamely from here

Process process = new ProcessBuilder("C:\\PathToExe\\MyExe.exe","param1","param2").start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;

System.out.printf("Output of running %s is:", Arrays.toString(args));

while ((line = br.readLine()) != null) {
System.out.println(line);
}

More information here

Other issues on how to pass commands here and here

Execute external program from Java

To get the redirection to work as written, you need to do this:

Process p = Runtime.getRuntime().exec(
new String[]{"/bin/bash", "-c", "ls > OutputFileNames.txt"});

The problem you were running into is the simple-minded way that that Runtime.exec(String) splits a command line into arguments.

If you were to run that command (as is) at a shell prompt, you would have had to have entered it as:

$ /bin/bash -c "ls > OutputFileNames.txt"

because the "-c" option for "bash" requires the command line for the spawned shell as a single shell argument. But if you were to put the naked quotes into the Java String, the Runtime.exec(String) method still get the splitting wrong. The only solution is to provide the command arguments as an array.

Executing external program in java and passing commands

a) how to bind a windows console application with java application?

link provided by the courtesy of Google search query:

https://www.google.pl/search?q=java+binding+console+to+an+app&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a

b) in short:

 InputStream is = p1.getInputStream();
OutputStream os = p1.getOutputStream();

(supplied by the obvious http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Process.html)

How do I run a Java program from the command line on Windows?

Source: javaindos.

Let's say your file is in C:\mywork\

Run Command Prompt

C:\> cd \mywork

This makes C:\mywork the current directory.

C:\mywork> dir

This displays the directory contents. You should see
filenamehere.java among the files.

C:\mywork> set path=%path%;C:\Program Files\Java\jdk1.5.0_09\bin

This tells the system where to find JDK programs.

C:\mywork> javac filenamehere.java

This runs javac.exe, the compiler. You should see nothing but the
next system prompt...

C:\mywork> dir

javac has created the filenamehere.class file. You should see
filenamehere.java and filenamehere.class among the files.

C:\mywork> java filenamehere

This runs the Java interpreter. You should then see your program
output.

If the system cannot find javac, check the set path command. If javac
runs but you get errors, check your Java text. If the program
compiles but you get an exception, check the spelling and
capitalization in the file name and the class name and the java
HelloWorld command. Java is case-sensitive!

External program call through java

I guess the terminal/session which jvm executes your command doesn't know where/what is 'ns' [i.e: your executable library]

Try executing by giving the full path to your library, like

Process p = Runtime.getRuntime().exec( "/fullpath/ns /home/maria/ns-allinone-2.35/ns-....

How to provide java program with external files when executing the run command in console?

One of the best command-line utility libraries for Java out there is JCommander.

A trivial implementation based on your thread description would be:

public class javaprogram {

@Parameter(names={"-file"})
String filePath;

public static void main(String[] args) {
// instantiate your main class
javaprogram program = new javaprogram();

// intialize JCommander and parse input arguments
JCommander.newBuilder().addObject(program).build().parse(args);

// use your file path which is now accessible through the 'filePath' field
}

}

You should make sure that the library jar is available under your classpath when compiling the javaprogram.java class file.

Otherwise, in case you don't need an utility around you program argument, you may keep the program entry simple enough reading the file path as a raw program argument:

public class javaprogram {

private static final String FILE_SWITCH = "-file";

public static void main(String[] args) {
if ((args.length == 2) && (FILE_SWITCH.equals(args[0]))) {
final String filePath = args[1];
// use your file path which is now accessible through the 'filePath' local variable
}
}

}


Related Topics



Leave a reply



Submit