• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Running unix command using java shows error

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic