Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Runtime.getRuntime.exec() problem

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Being a smart alec beats the alternative. This tiny ad knows what I'm talking about:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic