• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1184
  • Last Modified:

Executing unix command in java using semicolon

Hi,

I want to execute 2 commands in one go, in unix this works like this:
ll ; cd ..

But when I call unix from out of java :
Process p = Runtime.getRuntime().exec("ll ; cd ..");

It threads the ; not as a delimiter for the previous command, but just like a character.
this results in ll semicolon, which gives of course the error message:
"; not found"
instead of executing ll and then cd ..

Any idea?

Tim
0
thimerion
Asked:
thimerion
  • 5
  • 3
  • 3
  • +2
5 Solutions
 
CEHJCommented:
You need to invoke the shell intepreter, especially since the first command is an alias

Runtime.exec("/bin/bash ...................
0
 
CEHJCommented:
Furthermore, cd is a shell built-in, not a programme. Make sure that you're aware of the issues here too:

http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
0
 
thimerionAuthor Commented:
Hi,
Sorry to have mislead you ...
I made the commands simpler for the example , but what I really try to execute is:
String cmd = new String("mv " + sourceDir + "/" + Message.getInputFname() + " " + targetDir
           + " ; ln " + targetDir + "/" + tsMessage.getInputFname() + " " + temparchInDir + "/" + Message.getInputFname());
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
aman123_123Commented:
Hi Thimerion,
You can achieve this by passing a array of Strings to the exec() function. Each element of the array will contain one command.

Hope this helps,
Aman

0
 
CEHJCommented:
>>but what I really try to execute is

That's OK. It's probably better to invoke the interpreter anyway, just in case. Using an array is always better than using a String since escaping is handled better. Each element would contain an argument
0
 
CEHJCommented:
Personally i'd split the commands up into two invocations. Redirecting, you might be able avoid handling the streams by doing something like:
String[] cmd1 = new String[] { "/bin/bash", "mv", sourceDir + "/" + Message.getInputFname(), targetDir, "2>&1 >/dev/null" };
Runtime.getRuntime().exec(cmd1);
 
// Same principle for the ln command

Open in new window

0
 
objectsCommented:
easiest would be to put two in a script and call scriipt using Runtime.exec.
Simpler code, and easier to mainain allowing you to change the commands without recompiling

0
 
thimerionAuthor Commented:
Hi,

I agree it is easier to just execute them one by one, but I want to save execution time.

Tried passing a array of Strings but that didn't work for me.  Also I guess that using that array of Strings is going to invoke the command interpreter twice.

I was looking for a way to escape the ; character so that it is back to the right meaning at unix level.

Regards,
Tim
0
 
objectsCommented:
try:

bash -c 'll;cd'

0
 
objectsCommented:
or as a array of strings as {"bash", "-c", "ll;cd"}
0
 
CEHJCommented:
>>Also I guess that using that array of Strings is going to invoke the command interpreter twice.

No - just once

>>I was looking for a way to escape the ; character so that it is back to the right meaning at unix level.

You don't escape it. If you want to use it, just pass it as one of the elements of the array.

If you're having a problem, be sure to read the link i posted. The stream redirection i showed you is a kludge - if there is an error at the shell, you'll never see it
0
 
gheistCommented:
Java has its own file access facilities.
0
 
thimerionAuthor Commented:
Sorry haven't had time yet to review it because I am put on another project, I'll accept the solutions that I think that will work,
thanks guys!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

  • 5
  • 3
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now