aspose file tools*
The moose likes Servlets and the fly likes Runtime.getRuntime.exec() problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Runtime.getRuntime.exec() problem" Watch "Runtime.getRuntime.exec() problem" New topic
Author

Runtime.getRuntime.exec() problem

Yongping Wang
Ranch Hand

Joined: May 15, 2001
Posts: 40
I thought I could run a native system command like copy d:\files\tmp\test1.txt d:\labfiles\lab1\. in Runtime.getRuntime.exec("copy d:\\files\\tmp\\test1.txt d:\\labfiles\\lab1\\.") but unfortunately I always got runtime exceptions. I just can't figure out why it failed. Thanks for help. -Yongping
Dave Soto
Ranch Hand

Joined: Sep 15, 2001
Posts: 55
If you post your code, and then post the error messages you are getting, we may be able to help...
However, it's considered bad form to use native commands from Java programs when there are ways to perform the same thing in Java. (And there are definitely ways to copy a file using 100% java)
Additionally, is this something your are doing within a servlet? Because if not, then this is not the forum for that question...
Yongping Wang
Ranch Hand

Joined: May 15, 2001
Posts: 40
Yes, it will be in a servlet. but I use main function to test it first. here is part of code:
if(!checkfile.existfile(dirName,fileName))
{
try
{
Runtime rt = Runtime.getRuntime();
String cmd = "copy d:\\files\\tmp\\" + fileName + " d:\\labfiles\\lab1\\.";
rt.exec(cmd);

}catch (Exception ex)
{
ex.printStackTrace();
}
}
copy process happens only if the same file doesn't exist in the target folder.
here is the exception:
at java.lang.Win32Process.create(Native Method)
at java.lang.Win32Process.<init>(Win32Process.java:66)
at java.lang.Runtime.execInternal(Native Method)
at java.lang.Runtime.exec(Runtime.java:551)
at java.lang.Runtime.exec(Runtime.java:418)
at java.lang.Runtime.exec(Runtime.java:361)
at java.lang.Runtime.exec(Runtime.java:325)
at CheckCopyFile.main(CheckCopyFile.java:15)
100% java code can do such task but it is sort of time-consuming.
Thanks.
Yongping
[This message has been edited by Yongping Wang (edited September 24, 2001).]
shashank bapat
Ranch Hand

Joined: Jul 11, 2001
Posts: 69
Originally posted by Yongping Wang:
100% java code can do such task but it is sort of time-consuming.
Thanks.
Yongping

Use JNI. Or program in C++ if u think copy is time consuming. System.exec should always be avoided when options are available.

-Shashank<BR>MS(CS),BS(CS),SCJP2
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Runtime.getRuntime.exec() problem
 
Similar Threads
File(s) copying through JAVA APIs
How to copy a file to another in a different directory?
Using a method from one program to work in another by importing
NoClassDefFoundError
Comparing two huge files