0

i need to execute any file using java program.like in jdk we have java,javac...like that

URL url = new URL("http://torrentz.eu/announcelist_116568555"); url.openConnection(); InputStream reader = url.openStream(); FileOutputStream writer = new FileOutputStream("t1.txt"); byte[] buffer = new byte[153600]; int bytesRead = 0; while ((bytesRead = reader.read(buffer)) > 0) { writer.write(buffer, 0, bytesRead); buffer = new byte[153600]; } writer.close(); reader.close(); String[] cmd = new String[1]; cmd[0]="t1.txt"; Process p = Runtime.getRuntime().exec("C:\\Documents and Settings\\INTEL\\My Documents\\NetBeansProjects\\urldemo\\t1.txt"); p.destroy(); } } 

here is list of errors

Exception in thread "main" java.io.IOException: Cannot run program "C:\Documents": CreateProcess error=2, The system cannot find the file specified at java.lang.ProcessBuilder.start(ProcessBuilder.java:1041) at java.lang.Runtime.exec(Runtime.java:617) at java.lang.Runtime.exec(Runtime.java:450) at java.lang.Runtime.exec(Runtime.java:347) at urldemo.Urldemo.main(Urldemo.java:58) Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified at java.lang.ProcessImpl.create(Native Method) at java.lang.ProcessImpl.<init>(ProcessImpl.java:376) at java.lang.ProcessImpl.start(ProcessImpl.java:136) at java.lang.ProcessBuilder.start(ProcessBuilder.java:1022) 
1
  • In your exec call, there should be called a program to open your .txt file. Commented Jul 23, 2013 at 6:07

2 Answers 2

2

Once you get over the path problem you'll see that you can't execute a text file on windows, because text files are not executable programs. If you want to open the file rather than execute it use the Desktop class. See for example How to launch the default (native) application for a given file from Java?

File file = new File ("c:/documents and settings/Intel/whatever/file.txt"); Desktop.getDesktop().open(file); 
Sign up to request clarification or add additional context in comments.

7 Comments

and what i have to do if i want to execute the exe file
First you need an exe file, and it has to be called something.exe and not something.txt
i know sir.. i have an exe with the same name and in the same directory,, si i just rename the file extension to .exe... and now plz explain how to execute this exe
and i am trying your getDesktop method but its showing me error that argument required File but found String
Oh, that's true, fixed. And the way you execute an exe file in your directory is the same as in any directory: with the code in your question but using the .exe file extension instead of .txt
|
0
Process p = Runtime.getRuntime().exec("cmd /c start notepad C:\\Documents and Settings\\INTEL\\My Documents\\NetBeansProjects\\urldemo\\t1.txt"); 

//you can also provide other editor instead of notepad

3 Comments

bro what i have to do, if i wanna execute an exe in the same directory with the same name??
Runtime.getRuntime().exec("C:\\Documents and Settings\\INTEL\\My Documents\\NetBeansProjects\\urldemo\\t1.exe");
i did this.. but its not working,, firstly my program was to execute an exe but it was not working for me

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.