Skip to main content
gamedev.net
🔒 Locked

[java] [java] Executing system calls? (like copy, del, etc.)

Started by nomadph Oct 10, 2006 at 4:54 AM 5 replies 1.7k views
Original Post
nomadph
nomadph
How to call system calls like "del myFile.txt" through Java? i've tried:

Runtime.getRuntime().exec("del myFile.txt"); 
But I get:

Error: java.io.IOException: CreateProcess: del myFile.txt error=2
Is there any other way to make system calls? thanks!
nomadph
nomadph
btw, what i'm trying to do is call ffmpeg through java...

thanks.
nomadph
nomadph
i'm not really trying to delete a file, i only used "del myFile.txt" as an example...

i'm trying to call ffmpeg (video conversion) executable file. I can call it on command/DOS prompt, but don't know how in java.

i've tried this:

  private void runFFMPEG(String aSrcVideo) {    //String[] cmd = {"cmd /c ffmpeg -i " + aSrcVideo + " -ar 11025 -ab 32 -f flv -s 450*340 " + aSrcVideo + ".flv"};    String cmd = "ffmpeg -i video.flv -ar 11025 -ab 32 -f flv -s 450*340 video_converted.flv";    Process p = null;    try {      p = Runtime.getRuntime().exec(cmd);      p.waitFor();      BufferedReader in = new BufferedReader( new      InputStreamReader(p.getInputStream()));       String line=null;      while((line=in.readLine() )!=null) {        System.out.println(line);      }    } catch( Exception e) {      System.out.println("hello:" + e);    }  }



but I get this:

File input error

nomadph
nomadph
*bump*
Michael Nischt
Michael Nischt
I guess your command works well on the command line shell?! (input: aSrcVideo; output: aSrcVideo.flv)

just an idea, which is probably wrong..

but could it be that the working directory doesn't match with the local path to myFile.txt or aSrcVideo.flv ?
try using an absolut path or check wth System.getProperty("user.dir");

Anyway, I would recommend you to use the ProcessBuilder class, since it offers a bit more control.


nmi
nmi
Runtime.exec() will start a new process. To start a process, there has to be some executable file on the disk that can be loaded.

Many commands you can enter in the shell are not an executable, but a function that is recognized by the shell itself. One of those functions is del. If you want to delete a file this way, you have to start the shell (cmd.exe) passing the command along with the other parameters.

Running ffmpeg via Runtime.exec() should be fine, since ffmpeg.exe is an executable file. You may want to specify the complete path to make it work.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.