Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Running an executable

  Asked By: Bill    Date: Jul 02    Category: Java    Views: 807
  

Does anyone know how to run an executable (say a c++ program) from
java and return, say a string back to the java file??

I have this code that can run an executable (in this case, it runs
NOTEPAD), but i wanted to know how i would be able to return
something:


import java.lang.System;
import java.lang.Runtime;
import java.io.IOException;

public class loadNotepad {
public static void main(String args[]) throws IOException {
Runtime r = Runtime.getRuntime();

Process p = null;

try {
p = r.exec("C:\\windows\\notepad.exe");
}
catch (Exception e) { }
}
}


Share: 

 

4 Answers Found

 
Answer #1    Answered By: Kerry Wright     Answered On: Jul 02

Have you looked at what P contains? Its possibly just the PID of the process,
but you never know.

 
Answer #2    Answered By: Miriam Green     Answered On: Jul 02

Yeah, it just gives the process  ID... i think i might save the
result of the c++ program  (for example a string) to a text file and
see if java  can read the text file, all in one process :) :)

 
Answer #3    Answered By: Alberta Miller     Answered On: Jul 02

I found this code  on the web. I believe it is what you are looking for?

import java.io.*;

public class  RunCommand {

public static  void main(String args[]) {

String s = null;

try {

// run  the Unix "ps -ef" command

//Process p = Runtime.getRuntime().exec("ps -ef");
Process p = Runtime.getRuntime().exec("date");

BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));

BufferedReader stdError = new BufferedReader(new
InputStreamReader(p.getErrorStream()));

// read the output from the command

System.out.println("Here is the standard output of the command:\n");
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}

// read any errors from the attempted command

System.out.println("Here is the standard error of the command (if
any):\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}

System.exit(0);
}
catch (IOException e) {
System.out.println("exception happened - here's what I know: ");
e.printStackTrace();
System.exit(-1);
}
}
}



// Copyright 1998 DevDaily Interactive, Inc. All Rights Reserved.

import java.io.*;

public class RunCommand {

public static void  main(String args[]) {

String s = null;

try {

// run the Unix "ps -ef" command

//Process p = Runtime.getRuntime().exec("ps -ef");
Process p = Runtime.getRuntime().exec("date");

BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));

BufferedReader stdError = new BufferedReader(new
InputStreamReader(p.getErrorStream()));

// read the output from the command

System.out.println("Here is the standard output of the command:\n");
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}

// read any errors from the attempted command

System.out.println("Here is the standard error of the command (if
any):\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}

System.exit(0);
}
catch (IOException e) {
System.out.println("exception happened - here's what I know: ");
e.printStackTrace();
System.exit(-1);
}
}
}

 
Answer #4    Answered By: Debbie Reyes     Answered On: Jul 02


try {
// Execute command
String command = "ls";
Process child = Runtime.getRuntime().exec(command);

// Get input stream to read from it
InputStream in = child.getInputStream();
int c;
while ((c = in.read()) != -1) {
process((char)c);
}
in.close();
catch  (IOException e) {
}


sending input

try {
// Execute command
String command = "cat";
Process child = Runtime.getRuntime().exec(command);

// Get output stream to write from it
OutputStream out = child.getOutputStream();

out.write("some text".getBytes());
out.close();
} catch (IOException e) {
}

 
Didn't find what you were looking for? Find more on Running an executable Or get search suggestion and latest updates.




Tagged: