This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Runtime.getRuntime.exec() problem

 
Yongping Wang
Ranch Hand
Posts: 40
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 55
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 69
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic