Hi guyz.
My challenge is that I can manually run the following command from linux shell:

sh /usr/bin/sshlogin.sh 192.168.0.37|danga|dan1ga|java -jar /shared/smsfiles/coin/MySSH.jar

successfully.

The challenge from java is getting the same command to run.
The snippet of my code that handles this is as follows, though it does not run as expected:

public void runCron(String job)
    {
        System.out.println("Inside runCron...");
        Runtime r = Runtime.getRuntime();
        Process p = null;
        String opt1 = "sh", opt2 = "/usr/bin/sshlogin.sh", opt3 = "192.168.0.37|danga|dan1ga|java"
                ,opt4= "-jar",opt5="/shared/smsfiles/coin/MySSH.jar";

        String [] commands = {opt1, opt2, opt3, opt4, opt5};

        if(job != null)
        {
            try
            {
                p = r.exec(commands);
                BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
                String str = "";
                while ((str = br.readLine()) != null)
                {
                    System.out.println(str);
                }
                br.close();
                p.waitFor();
            } catch (Exception e)
            {
                System.out.println("Exception "+e.getMessage());
                System.err.println("Exception "+e.getMessage());
            }
        }

    }//runCron

I'm guessing maybe that the pipe character ,|, is the root cause of this whole issue but then again I may be wrong.
Someone kindly assist me.
Thanks you all.

Recommended Answers

All 3 Replies

Since Java 1.5 the preferred way to run external processes is the new ProcessBuilder class. You may find that simple switching to that will improve things

http://www.java-tips.org/java-se-tips/java.util/from-runtime.exec-to-processbuilder.html

Hi James, I tried out switching to

Process p = new ProcessBuilder(commands).start();

but no change to my output.Anyway, thanks James.

I still think that Java as I've used it with the command that has the pipe character, is not able to replicate the manual running of the command on the shell.
Can anyone else assist, please.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.