-2

Maybe it looks simple question, but I couldn't find appropriate answer. I am wondering is it possible to run .bat or .exe file from text file, for example, 1st we will retrieve the File object from the file chooser, then grabbing its path and throwing it into the text file. After by reading this path from text file (find a location of program), execute .bat or .exe program? For example, if 1st file in location: Libraries\Documents\pro.bat and location of 2nd file: E:\pro2.exe. Then by storing this path to text file test.txt:

Libraries\Documents\pro.bat
E:\pro2.exe

after execute it?

Actually I have done this parts: - retrieve the File object from the file chooser and save its path to text file. But I don't know how to run it from text file. Can someone give me a right direction. Any help is really appreciated.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
hub
  • 101
  • 10
  • 1
    Text files aren't supposed to be executable. I don't quite understand the question. – OneCricketeer Nov 30 '15 at 08:55
  • 1
    txt files are not executables. You can't run anything "from" them. Since you have the path, why bother to write it into a txt file? why not just run it? – Stultuske Nov 30 '15 at 08:55
  • I suppose OP wants to run all files (.bat or .exe) listed within one textfile. – Manu Nov 30 '15 at 08:55
  • This should help: http://stackoverflow.com/questions/2243993/how-to-execute-command-line-exe-file-in-java – Manu Nov 30 '15 at 08:57
  • @Manu the way I understand the text: the application has all the records, writes it down in a txt file, and then tries to run it. Since the application already has everything ... why bothering reading it from the text file in the first place? – Stultuske Nov 30 '15 at 08:58
  • before to run program I want to fix their location and then run it, what is the best way to store the path and then execute it? – hub Nov 30 '15 at 08:59

2 Answers2

2

First of all, you can't run txt files (they are not executable). Secondly, you can get path of external application form a text file and then you can use that string to run external application by using

Runtime rt = Runtime.getRuntime();
Process p = rt.exec(cmd);
edt
  • 92
  • 2
  • 5
1

Not quite sure what your program should do. But, as far as I understand your question you would like to choose (form a file chooser) a number of executables and then these executables should run sequentially (?).

You don't need to store these paths into a text file. Just store them in memory for example in a List<String> or List<File>. Then you can manipulate the paths (as mentioned in your comment) and run them as edt pointed out with:

Runtime rt = Runtime.getRuntime();
for (String execPath : listOfExecs) {
    Process p = rt.exec(execPath);
    p.waitFor();
}
Community
  • 1
  • 1
Mathias Begert
  • 2,354
  • 1
  • 16
  • 26
  • yes, they have to run sequentially. And the path to the file can be different in user directory, so I don't know exact path of file. In this case possible to use `List` for that purpose? – hub Nov 30 '15 at 10:06
  • Sorry for the long delay. Just now could return to this problem and @Mathias Begert thank you for your answer. It works very well!! I accept and +1. – hub Oct 25 '16 at 06:29