Results 1 to 8 of 8
  1. #1
    Join Date
    Feb 2011
    Posts
    10
    Rep Power
    0

    Default Executing Command line commands from java - problem with deleting a file

    Hi

    The Command line execution function works fine when I try to run a program by this code ( there is more of it but this shows what I am using )

    Runtime rt = Runtime.getRuntime();
    Process p = rt.exec(command);
    int exitVal = p.waitFor();

    BUT when I try to make java delete a file by saying command="del file.txt"
    I use Windows by the way

    It does not do it!!! I dont know why, here is the exception I get :

    java.io.IOException: Cannot run program "del": CreateProcess error=2, The system cannot find the file specified

    Basically there is no file called "del" yes I get it... But why doesn't it take the command as whole? How can I make it delete a file?

    Thank you

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    Have a read of the (famous) JavaWorld Traps and Pitfalls article. Yours looks like that discussed on page 4. (commands are not executables)

  3. #3
    HarithaReddy is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default The method delete in java.io.File

    It is better to use the method delete() in the class File to delete a file, rather open creating a separate process to delete the file,

  4. #4
    HarithaReddy is offline Member
    Join Date
    Feb 2011
    Posts
    5
    Rep Power
    0

    Default Command string when deleting file as process

    The command="del file.txt" should be "cmd /c del file.txt"

  5. #5
    Join Date
    Feb 2011
    Posts
    10
    Rep Power
    0

    Default

    Thanks a lot to all, this helped me solve the problem and understand why it didnt work !

    One more question about the same Runtime.exec function :

    I am also trying to run GNU-prolog ( interactive programming language )using Runtime.exec function. When I run prolog, in the command line it starts runing and then it interactively expects arguments from me.

    Using

    Runtime rt = Runtime.getRuntime();
    Process p = rt.exec("gprolog");

    I can make prolog run ( I dont really know if it runs but the command does not throw an exception) but then how can I make it take a second argument that will be processed by prolog?

    I don't know if I could explain what I meant clearly but here is what would happen if I was trying to use prolog from command line.

    First command I would use would be to set an environment variable to make prolog run in command line, without a GUI
    >SET LINEDIT='gui=no
    Then I run prolog
    >gprolog
    Then in the command line it tells me prolog is running by telling

    C:\Users\EFE>gprolog
    GNU Prolog 1.3.1
    By Daniel Diaz
    Copyright (C) 1999-2009 Daniel Diaz
    | ?-

    where ?- expects me to give a command to prolog

    Then I consult my program
    >consult(prolog.pl).

    This is all I am trying to do

    Thanks for all the replies again

  6. #6
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Java Code:
    InputStream in = p.getInputStream();
    InputStream err = p.getErrorStream();
    OutputStream out = p.getOutputStream();
    These three streams are how you interract with a subprocess. You can read from 'in' to receive the information that gprolog would otherwise send to the terminal, and you can use 'out' to write to gprolog as if you had typed it in in the terminal. There are a lot of gotchas doing what you're trying to do. The readers for 'in' and 'err' need to be in separate threads to prevent deadlock conditions. You need to handle all excpetions and make sure that no matter what happens, these streams get closed (they are open even if you don't call 'get{X}Stream' !), and make sure you destroy the process when it completes to free all other hidden resources.

  7. #7
    Join Date
    Feb 2011
    Posts
    10
    Rep Power
    0

    Default

    I can't really say I understand this. But I don't need to take any message back from prolog, as I understand only using the output stream to send a command to prolog will be enough for me(once that command works, prolog will output a text file anyways). Can you post an example on how I use the output stream to send a command to process p ?

    Sorry until now I have never used output stream for something like this so I am clueless :)

    Thanks

    --- Another way to do this ( maybe )
    I also need to set environment variables for this process. Maybe its better to use processBuilder in this case? Personally I have never used the processBuilder I was searching how to set environment variables from java and this seems to be the only way. Can I output my commands to prolog using the processBuilder too ?
    Last edited by efebatistaarda; 02-10-2011 at 06:28 PM.

  8. #8
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Java Code:
    p.getOutputStream().write(new String("This is the same as typing on the terminal window\n").getBytes("utf-8"));
    You absolutely *must* handle the input and error streams. If you don't, the pipes will eventually fill up and your application will deadlock.

Similar Threads

  1. problem in executing linux command
    By Basit56 in forum New To Java
    Replies: 1
    Last Post: 02-08-2010, 04:53 PM
  2. [SOLVED] deleting a line from a text file
    By mokonji in forum New To Java
    Replies: 0
    Last Post: 03-10-2009, 02:35 PM
  3. Error Occurred Executing Command Line Solution
    By RahulKhire in forum Eclipse
    Replies: 1
    Last Post: 09-10-2008, 02:38 PM
  4. Replies: 0
    Last Post: 06-20-2008, 10:02 AM
  5. Problem during executing Command Prompt
    By keshari in forum Advanced Java
    Replies: 4
    Last Post: 06-05-2008, 05:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •