abcd abcd - 1 year ago 70
Java Question

How to get Output of any project on button click event

I am working on Jframe Form in NetBeans. Actually my problem is .........
I have a List, button, Textbox and a TextArea. In list, there are many projects(like Update, Sum).
When I select a project from the list, The input of this list will be shown in Textbox(like Update.jar , Sum.jar) . When I execute jar file, the output of jar file should be shown in TextArea. How can I do that . Anyone Please help me.
Thanks in Advance.

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

String a= String.valueOf(jList1.getSelectedValue());

jTextArea1.setText(String.valueOf(Runtime.getRuntime().exec("java -jar "+ a+".jar")));

} catch (Exception e) {

Code of Sum.jar

package sum;

public class Sum {

public static void main(String[] args) {

int a=10,b=90;

int s=a+b;

System.out.println("Sum of two numbers:"+s);


Answer Source

I think this is what you want to do. This method runs a jar file and return its output.

public String getOutputOf(String jarfile) throws IOException{
    ProcessBuilder builder = new ProcessBuilder ("java", "-jar",  jarfile); 
    Process process = builder.start();             
    BufferedReader reader = new BufferedReader (new InputStreamReader(process.getInputStream())); 
    StringBuilder sb = new StringBuilder();
    String line = "";
    while ((line = reader.readLine()) != null) {
        sb.append(line + "\n");
    return sb.toString();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download