Sponsored Link •
|
Advertisement
|
Advertisement
|
This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.
Message:
Hmm... Maybe I don't understand exactly what the problem is. It seems fairly straight-forward to me, especially in the case where you just want to launch a player or something, because all you have to do is launch it and forget about it. You would only get into all the streaming stuff mentioned in the article if you wanted to send input to and get output from a console app. By the way, the article mistakenly asserts that if you want to do redireection, you must do it programmatically. Really, all you have to do is form the exec() string correctly. For instance, this works correctly: "c:\command.com /c echo Test 123 > c:\temp\exectest.txt" (try it in the app below and then check that the output file was created). I also fooled around a bit with it on Linux (I was trying "bash ls > file.txt" and stuff like that), but didn't get an output file (maybe a unix expert can offer a suggestion on this). It just may be that Linux requires the String array format of exec() -- more on that below. As far as what I recommend, I'll give you my opinion for the Windows platform, where my expertise is: generally, it is better to do a "start file.mp3" than it is to try and launch a particular player. The start program is a little tool in Windows that kind of gives the effect of double-clicking the file in Explorer. If you use start, you'll get the player that the user has configured (for instance I may have MusicMatch installed, but I'd still rather have WinAmp be my default player). Furthermore, if I wanted to do some more fancy stuff with executing programs on the windows platform, I'd have my Java program dynamically build a JavaScript (or JScript, really) that would handle the whole thing. JScript (and VBScript, but after learning object-oriented languages like C++, Java and Python I can no longer stomach aesthetically reprehensible BASIC language) is built into the system, so you don't have to do anything more than "start myscript.js" to run it. Also, JScript easily can hook into COM automation and therefore the Windows system, so it is probably better to do your dirty work that way, if possible, than to hose up your Java code with native methods. If you are working on the Windows platform, it is worth learning some JScript (unless you can find a Python module for the Windows Script Host! If you do, let me know!). If you want to do all the input/output kind of thing discussed in the article and you don't know (and don't want to learn) JScript then it would be a good idea to wrap all that in a utility class. That would have the advantage of being a little more platform independent. Below is a little app I wrote to facilitate fooling around with Runtime.exec(). You can run it as a console app (no GUI), or as a Swing application. As a console app, just specify the program to run, like so: "java ExecTester Notepad myfile.txt". If you run with no parameters, or with "/showui", the GUI will appear and you can try lots of exec()s there. Note that I didn't bother with breaking the command into an array or adding environment stuff (as it works fine on the Windows platform as-is), but that could be done by someone with the time and ambition to do so. Then it would be more platform independent. Another addition, to make this thing complete would be adding all the process-monitoring stuff discussed in the article. I don't have time to do this, as I am still working on that homework assignment with the multiple grade options that I mentioned to Chin. (Maybe if one of these students requesting homework solutions does it, we can reciprocate by doing one of his/her homework assignments! :-) - mfg // I suppose "import javax.swing.*" would be more succinct, but /** /** // frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); Container pane = frame.getContentPane(); pane.add(new JLabel("Enter your command:")); pane.add( new JLabel( "Results of execution:" ) ); frame.setVisible(true); /** /** try return results + "\n"; /**
Replies:
|
Sponsored Links
|