How to run a java program in a separate process using GUI button?

M

misbah.mubarak

I want to run a java program using a button on the GUI. I have used
the following code
try{
System.out.println("Inside execution");
Runtime.getRuntime().exec("cmd.exe");
System.out.println(command);
process= new ProcessBuilder(command);
process.start();
}
catch(Exception except)
{
except.printStackTrace();
}
The purpose is to launch a separate execution window a part from the
cmd.exe thats open through the GUI. When I did this using process
builder,the output would come but not in a separate execution window.
Moreover, the output was not complete and the println statements were
not getting displayed.
 
G

gethostbyname

I want to run a java program using a button on the GUI. I have used
the following code
try{
System.out.println("Inside execution");
Runtime.getRuntime().exec("cmd.exe");
System.out.println(command);
process= new ProcessBuilder(command);
process.start();}

catch(Exception except)
{
except.printStackTrace();}

The purpose is to launch a separate execution window a part from the
cmd.exe thats open through the GUI. When I did this using process
builder,the output would come but not in a separate execution window.
Moreover, the output was not complete and the println statements were
not getting displayed.

Please, post your complete code. What about command and process
variables?

You could use System.err to get the println statements displayed. No?

gethostbyname
 
G

gethostbyname

I want to run a java program using a button on the GUI. I have used
the following code
try{
System.out.println("Inside execution");
Runtime.getRuntime().exec("cmd.exe");
System.out.println(command);
process= new ProcessBuilder(command);
process.start();}

catch(Exception except)
{
except.printStackTrace();}

The purpose is to launch a separate execution window a part from the
cmd.exe thats open through the GUI. When I did this using process
builder,the output would come but not in a separate execution window.
Moreover, the output was not complete and the println statements were
not getting displayed.

You could use System.err to get the println statements displayed.

gethostbyname
 
G

gethostbyname

I want to run a java program using a button on the GUI. I have used
the following code
try{
System.out.println("Inside execution");
Runtime.getRuntime().exec("cmd.exe");
System.out.println(command);
process= new ProcessBuilder(command);
process.start();}

catch(Exception except)
{
except.printStackTrace();}

The purpose is to launch a separate execution window a part from the
cmd.exe thats open through the GUI. When I did this using process
builder,the output would come but not in a separate execution window.
Moreover, the output was not complete and the println statements were
not getting displayed.

I think I understood your problem now. Would you like "redirect" the
output stream of external process?


********
package javaapplication2;

import java.awt.*;
import java.io.*;
import javax.swing.*;

/**
*
* @author gethostbyname
*/
public class Main {


public Main() {
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
ProcessBuilder process;

try{
String command = "c:\\WINDOWS\\system32\\rasdial.exe";
InputStream in =
Runtime.getRuntime().exec(command).getInputStream();

String linha;
BufferedReader entrada = new BufferedReader(new
InputStreamReader(in));
while ((linha = entrada.readLine()) != null) {
System.out.println(linha);
}
entrada.close();

process = new ProcessBuilder(command);
process.start();
}

catch(Exception except)
{
except.printStackTrace();
}

}
}
********

gethostbyname
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top