aspose 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 | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
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: 6
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
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14268
    
  21

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 8 API documentation
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11402
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Rajesh Datla
Greenhorn

Joined: Mar 28, 2007
Posts: 6
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: 19718
    
  20

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 - OCEEJBD 6
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