File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Running unix command using java shows error Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Running unix command using java shows error" Watch "Running unix command using java shows error" New topic
Author

Running unix command using java shows error

Rajesh Datla
Greenhorn

Joined: Mar 28, 2007
Posts: 5
Hi All,
I am trying to run UNIX move command using Java exec method but its throwing error, unable to rename. Here below is the code i am trying to run to move all files from one directory to another directory. source and destination directory exists.



if i give "mv /home/demo1 /home/demo2" in exec method, its working, with * character its giving problem.

Please help me in resolving the problem.
Thank you
[ December 12, 2008: Message edited by: Martijn Verburg ]
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12924
    
    3

Normally, the it is the shell in which you type in a command like "mv /home/demo1/* /home/demo2" that replaces the * and other wildcard characters with whatever it should be replaced with.

When you call a command directly from Java, not through the Unix shell, it will tell the command to literally look for a file named *. That file doesn't exist, so it doesn't work.

There are better ways to move files from one directory to another. You can do this with the rename() method in class java.io.File, for example.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 9948
    
    6

Just a friendly tip... when you get an error, please post the EXACT and COMPLETE text of the error. It greatly helps others figure out the problem.


Never ascribe to malice that which can be adequately explained by stupidity.
Rajesh Datla
Greenhorn

Joined: Mar 28, 2007
Posts: 5
Thank you for your suggestion

error is: mv: 0653-401 Cannot rename /home/demo1/* to /demo2/*:
A file or directory in the path name does not exist

using below code helped to solve the problem:


[ December 15, 2008: Message edited by: Martijn Verburg ]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19216

That's because now you aren't executing mv - you are executing sh, and in that shell you are executing mv.


SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Running unix command using java shows error
 
Similar Threads
Running Java app with exec() method
How to run .java file from Runnable
Runtime Error code 9
How to use JavaDocs and Doclets??
Compile and create instance of a new java program from existing java program