LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-24-2004, 11:12 PM   #1
mcshen
LQ Newbie
 
Registered: Dec 2003
Distribution: womandrake
Posts: 28

Rep: Reputation: 15
java runtime() exec() program


i am trying to copy all files from a directory to another

i have
---------
try {
// Execute a command with an argument that contains a space
String[] commands = new String[3];
commands[0]="cp";
commands[1]="/student5/h4/03/pc/ab07/assignmentfolder/nha/s1/*";
commands[2]="test";

Process child = Runtime.getRuntime().exec(commands);
} catch (IOException e) {
System.err.print(e.getMessage());
}

but this does not seem to work. test is the targer directory
the command works in linux. but won't work when i execute it in java.

any idea?
thanks in advance.

Last edited by mcshen; 11-24-2004 at 11:17 PM.
 
Old 11-24-2004, 11:21 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
exec ("string")?

What happens if you try just one string (i.e. 'exec ("cp from/*.* /to")')?
 
Old 11-24-2004, 11:25 PM   #3
Jose Muņiz
Member
 
Registered: Jul 2003
Location: Mexico City
Distribution: Slackware 9.1, SuSE 9.1
Posts: 248

Rep: Reputation: 32
Quote:
commands[2]="test";
You are copying the contents of the other directory into this one. Why don't you include the absolute path instead? If it works, then maybe you're just executing your Java application from the wrong place, which makes the program unable to access the directory through a relative path..

If it fails, while not set an Error Stream so that you can know what's going on?
 
Old 11-25-2004, 09:48 AM   #4
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994

Rep: Reputation: 76
Re: java runtime() exec() program

Quote:
commands[1]="/student5/h4/03/pc/ab07/assignmentfolder/nha/s1/*";
This is just a guess, but * globs are usually unescaped by the shell. You may in fact be trying to copy a single file of name * from s1/ to test/, rather than un-globbing the filenames of the individual files.
Try this:
Code:
commands[0]="/bin/bash";
commands[1]="-c";
commands[2]="cp /student5/h4/03/pc/ab07/assignmentfolder/nha/s1/* test";
This will explicitly execute commands[2], using a BASH shell as an interpreter.
 
Old 11-25-2004, 11:29 AM   #5
mcshen
LQ Newbie
 
Registered: Dec 2003
Distribution: womandrake
Posts: 28

Original Poster
Rep: Reputation: 15
cool
it works
thanks
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Java Programming: Java Runtime Environment not found when trying to compile murbz Linux - Software 2 03-26-2009 03:04 AM
java runtime problem iamedu Programming 2 09-23-2004 06:00 PM
Runtime.getRuntime().exec("javac ") ihasircioglu Programming 1 05-27-2004 06:54 AM
java runtime shariqali Linux - Software 7 09-23-2003 10:08 PM
Java Runtime 1.3 on Redhat 7.2??? wildsider Linux - Software 3 02-05-2002 11:36 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:03 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration