running unix command from java

Hi All,

I have been struggling for a week trying to run a unix command from my java program.

the unix command is:

ssh dmdev3@tsapid01-zt3d01 ':> /t3/envs/dmdev3/test/file_list.txt'

when i try to run this command directly on my unix console, it works perfectly.

but when i try it form my JAVA program, i get the below mentioned error

ksh: :> /t3/envs/dmdev3/test/file_list.txt:  not found

:(:(:frowning:

below is my java code snippet that i use,

procTemp=runEnv.exec(strShellCmd);

where strShellCmd contains the entire unix command as a single string

requiring your help at the earliest cause i'm in such a desperate situation.

Thanks in advance,
Madhu.

Please don't cross post the same question across multiple forums, it dilutes effort.

i am sorry. was a bit confused on the correct location of the topic.
rest assured it wont happen again. :slight_smile:

Let's see that line too.
First impression is that the command would be better placed in Shell Script.

What (in words) are you expecting the command to do. It appears to just execute a null command on a remote server.

The /t3/envs/dmdev3/test/file_list.txt file is (should be) cleared by this command.

private boolean executeShell(String strShellCmd) {
        try {
            procTemp=runEnv.exec(strShellCmd);

            //read the output from shell console
            BufferedReader brShellOP = new BufferedReader(
                    new InputStreamReader(procTemp.getInputStream()));
            String strShellOP = null;
            while ((strShellOP = brShellOP.readLine()) != null) {
                System.out.println(strShellOP);
            }
            brShellOP.close();

            // catch error from shell console (if any)
            BufferedReader brShellError = new BufferedReader(
                    new InputStreamReader(procTemp.getErrorStream()));
            String strShellError = null;
            while ((strShellError = brShellError.readLine()) != null) {
                System.out.println(strShellError);
            }
            brShellError.close();

            // wait for the process to finish
            try {
                intExitVal = procTemp.waitFor();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
            
            //returns false if shell console throws error
            if(intExitVal!=0){
                return false;
            }
            return true;

        } catch (IOException ioEx) {
            System.out.println("error in executeShell().IOException");
            ioEx.printStackTrace();
            return false;
        }
    }

this is the entire method that does the execute operation.
Let me know if you require any other inputs for getting my issue solved.

PS: i'm new to this executing-unix-from-java thing.

Thanks a tonne,
Madhu.

Use cp /dev/null /t3/envs/dmdev3/test/file_list.txt

@jlliagre

yes. it does clear the file. BUT only when i run from the unix console.
When the same is attempted from a java programme, it fails throwing at the earlier mentioned error.

---------- Post updated at 06:03 PM ---------- Previous update was at 05:53 PM ----------

@Peasant

I still get the same error :frowning:

 ksh: cp /dev/null /t3/envs/dmdev3/test/file_list.txt:  not found

This error message probably came from the following command:

ksh 'cp /dev/null /t3/envs/dmdev3/test/file_list.txt'
Or even just:
'cp /dev/null /t3/envs/dmdev3/test/file_list.txt'

i.e. It tries to execute a command with the name of all the characters between the single quotes including the space characters.
Just for interest, this would have worked:

ksh -c 'cp /dev/null /t3/envs/dmdev3/test/file_list.txt'

Something is going wrong with the quote characters on the ssh command line. I suggest placing the whole command in a Shell script, and then calling that Shell script from java.

Did we see the asignment of strShellCmd ?

It looks like you are trying to execute quoted commands:
"cp /dev/null /t3/envs/dmdev3/test/file_list.txt" or ":> /t3/envs/dmdev3/test/file_list.txt" instead of these unquoted ones, eg: : > /dev/null /t3/envs/dmdev3/test/file_list.txt .

@all

i have found out the solution. Though the shell command was right, the way i called it from JAVA caused the issue. The correct java code would be as shown below.

ProcessBuilder procBuild=new ProcessBuilder("bash","-c",strShellCmd);

This did the trick and now it works without a flaw for ANY SHELL COMMAND. :D:D

Thanks all,
Madhu.

Thank you for posting the solution.

While Googling I came across similar references, but they cross-linked to the old Sun Java site which Oracle in their wisdom have restructured such that old links don't work.

The problem is that the Java exec command tokenizes the command call and places each token onto the system call in order.

Thus:
exec(command)
is the same as
exec(command, null, null)

I'd still place the unix command in a Shell script (complete with appropriate Shebang line) because I can see potential for problems with quote characters and accidentally providing switches to the Shell command.

@methyl

yeah you are right about the tokenizing part. Also, bash command is requried as the primary parameter while running commands with more than two arguments.

Anyways can you please elaborate your last statement?

PS : Can you please tell me how to close/resolve this thread?

Thanks,
Madhu.