0
String[] command = { "cmd.exe", "/C", "Start", "skype" };
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(command);

Hello, I am trying to start skype in java! and i have this problem: I am using code above, In eclipse it works perfect but when i compile it into runnable jar it will stuck into infinite loop where new java processes are started until whole java crashes!

And the problem is in this part of code. (When i comment it everything works fine but skype is not started)

Where can be problem?

Edit*

I made small example of Program:

Main class:

package Client;

public class DemoMain {


    public static void main(String[] args) {
        new Demo();
    }

}

Process class:

package Client;

import java.io.IOException;

public class Demo {


    public Demo(){
        try {
            Runtime.getRuntime().exec(new String[]{ "cmd.exe", "/C", "Start", "skype" });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Result: Task Manager view

In eclipse compiler it works great! Problem is when program is started from Runnable jar

Martin Tomko
  • 155
  • 1
  • 1
  • 13
  • This code runs exactly that one command once. The only logical explanation is that **something** around this code is looping. You really really want to create a "minimal viable" example that gives you the error and post that complete code here. We can't know from looking at correct code what is wrong on your side! So please see the help center; to understand how you have to ask questions so that we have a chance giving you a helpful answer. – GhostCat Jul 31 '16 at 13:48

1 Answers1

0

Define a batch file first,and then call the batch file. define a method and do not use the constructor method.

    public static String os_exec(String[] cmds)
    {
        int ret = 0, i = 0;
        StringBuffer sb = new StringBuffer();
        Process process = null;
        String line = null;
        BufferedReader bufferedReader = null;
        try
        {
            ProcessBuilder pb = new ProcessBuilder(cmds);
            pb.redirectErrorStream(true);
            process = pb.start();
            pb.redirectErrorStream(true);
            bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            while ((line = bufferedReader.readLine()) != null)
            {
                i += 1;
                sb.append(line);
                if( i > 1 )
                {
                    sb.append("\r\n");
                }
            }
            ret = process.waitFor();
        }
        catch (Exception e)
        {
            StringBuffer err = new StringBuffer();
            for(String commond : cmds )
            {
                err.append(commond);
                err.append(' ');
            }
            sb.append(e.getMessage());
        }
        finally
        {
            if( bufferedReader != null )
            {
                try
                {
                    bufferedReader.close();
                    process.destroy();
                }
                catch (Exception e)
                {
                    sb.append(e.getMessage());
                }
            }
        }
        return sb.toString();
    }

start.bat

start C:\skype.exe

invoke os_exec();

String[] command = { "cmd.exe", "start.bat" };
os_exec(command);
Kevin Gao
  • 1
  • 3