Solved

Executing unix command in java using semicolon

Posted on 2008-06-23
13
1,053 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
  • 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
starter POM and spring-boot-starter,  spring-boot-web 2 54
expectj telnet failing 5 35
tomcat administrtor 12 46
sql import cannot be resolved jsp 3 17
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

813 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now