Operating System - HP-UX
1753834 Members
7861 Online
108806 Solutions
New Discussion юеВ

Re: command execution on HPUX from a Java process

 
SOLVED
Go to solution
Fedele Giuseppe
Frequent Advisor

command execution on HPUX from a Java process

Hello,

I need to execute a command (ls -1 /*) on HPUX platform through a Java process.

I have written the following test code:

String s = "ls -1 /tmp/prova*";

try {
Process proc = Runtime.getRuntime().exec(s);
BufferedReader read=new BufferedReader(new InputStreamReader(proc.getInputStream()));

while(read.ready())
{
System.out.println("line = " + read.readLine());
}

proc.destroy();

}catch(IOException e) {
e.printStackTrace();
}

but nothing is printed.

If I remove "*" character from the command:

String s = "ls -1 /tmp/prova";

it works fine.

How can I manage such character?

Thanks
4 REPLIES 4
x2084
Trusted Contributor

Re: command execution on HPUX from a Java process

This is not really a VMS related question, but it was answered here, some time ago, see threadId=1226409.

Bottom line, the wildcard is expanded by the shell, but you only run the ls program.

You may want to use something like "sh -c \"ls -1 /tmp/prova*\"". But be aware that this creates two processes, one for the shell and another one for ls.

Fedele Giuseppe
Frequent Advisor

Re: command execution on HPUX from a Java process

I have tried in this way and also using:

"sh -c 'ls -1 /tmp/prova*'"

but without success.
x2084
Trusted Contributor
Solution

Re: command execution on HPUX from a Java process

My fault, I thought the exec would pass the string as is, but it doesn't. It parses the string and constructs arguments.

Try to change
String s = "ls -1 /tmp/prova*";
to
String cmd[] = {
"sh",
"-c",
"ls -1 /tmp/prova*"
};
specifying explicit arguments.

Fedele Giuseppe
Frequent Advisor

Re: command execution on HPUX from a Java process

Now it works.
Many thanks