This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Runtime.getRuntime.exec() problem

 
Yongping Wang
Ranch Hand
Posts: 40
  • 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
  • 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
  • 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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic