Results 1 to 8 of 8
- 02-08-2011, 11:26 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 10
- Rep Power
- 0
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
- 02-09-2011, 12:47 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Have a read of the (famous) JavaWorld Traps and Pitfalls article. Yours looks like that discussed on page 4. (commands are not executables)
- 02-09-2011, 07:39 AM #3
Member
- Join Date
- Feb 2011
- Posts
- 5
- Rep Power
- 0
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,
- 02-09-2011, 07:49 AM #4
Member
- Join Date
- Feb 2011
- Posts
- 5
- Rep Power
- 0
Command string when deleting file as process
The command="del file.txt" should be "cmd /c del file.txt"
- 02-09-2011, 08:26 AM #5
Member
- Join Date
- Feb 2011
- Posts
- 10
- Rep Power
- 0
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
- 02-09-2011, 09:51 PM #6
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
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.Java Code:InputStream in = p.getInputStream(); InputStream err = p.getErrorStream(); OutputStream out = p.getOutputStream();
- 02-10-2011, 05:03 PM #7
Member
- Join Date
- Feb 2011
- Posts
- 10
- Rep Power
- 0
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 05:28 PM.
- 02-10-2011, 07:37 PM #8
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
Similar Threads
-
problem in executing linux command
By Basit56 in forum New To JavaReplies: 1Last Post: 02-08-2010, 03:53 PM -
[SOLVED] deleting a line from a text file
By mokonji in forum New To JavaReplies: 0Last Post: 03-10-2009, 01:35 PM -
Error Occurred Executing Command Line Solution
By RahulKhire in forum EclipseReplies: 1Last Post: 09-10-2008, 01:38 PM -
Problem while executing unix command having \&\& symbol
By suryahota in forum New To JavaReplies: 0Last Post: 06-20-2008, 09:02 AM -
Problem during executing Command Prompt
By keshari in forum Advanced JavaReplies: 4Last Post: 06-05-2008, 04:06 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks