Solved

Executing unix command in java using semicolon

Posted on 2008-06-23
13
1,075 Views
Last Modified: 2013-12-06
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
Comment
Question by:thimerion
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 3
  • +2
13 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 21845813
You need to invoke the shell intepreter, especially since the first command is an alias

Runtime.exec("/bin/bash ...................
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 21845829
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
 
LVL 1

Author Comment

by:thimerion
ID: 21845917
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 4

Accepted Solution

by:
aman123_123 earned 50 total points
ID: 21846111
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 21846337
>>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
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 100 total points
ID: 21847090
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
 
LVL 92

Assisted Solution

by:objects
objects earned 100 total points
ID: 21851153
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
 
LVL 1

Author Comment

by:thimerion
ID: 21853176
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
 
LVL 92

Expert Comment

by:objects
ID: 21853242
try:

bash -c 'll;cd'

0
 
LVL 92

Assisted Solution

by:objects
objects earned 100 total points
ID: 21853248
or as a array of strings as {"bash", "-c", "ll;cd"}
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 100 total points
ID: 21853519
>>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
 
LVL 62

Expert Comment

by:gheist
ID: 21873508
Java has its own file access facilities.
0
 
LVL 1

Author Comment

by:thimerion
ID: 21945765
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question