Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Running unix command using java shows error

 
Rajesh Datla
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15284
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12126
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rajesh Datla
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20532
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's because now you aren't executing mv - you are executing sh, and in that shell you are executing mv.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic