Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do i open a file with it's associated application using JAVA ? 4

Status
Not open for further replies.

Sun

Programmer
Aug 13, 1999
37
0
0
IN
Visit site
I would like to develop a explorer in Java,<br>
so in that explorer when I double click on any &quot;.doc&quot; file I would like MS WORD to fire, In fact how do i fire another<br>
using Java like u have &quot;WinExec&quot; or &quot;ShellExecute&quot; in Sdk/VC++ ?<br>
<br>
Am very keen to know. :)
 
Hello,<br>
<br>
You may do that with the help of the class java.lang.Runtime<br>
and it's method exec(String command). Your string may contain for instance &quot;C:\appl\winword.exe c:\myDoc.doc&quot;.<br>
<br>
Hope this helps, greetings,<br>
Alain V.<br>

 
import java.io.* ;<br>
import java.lang.Runtime ;<br>
<br>
public class RunApp<br>
{<br>
public static Runtime run ;<br>
public static void main(String args[])<br>
{<br>
System.out.println(&quot;Hello&quot;) ;<br>
try<br>
{<br>
run.exec(&quot;C:\\win98\\notepad.exe C:\\rock.txt&quot;) ;<br>
}<br>
catch ( IOException e)<br>
{<br>
System.out.println(&quot;Exception caught &quot; + e ) ;<br>
}<br>
catch ( NullPointerException ee)<br>
{<br>
System.out.println(&quot;Null Pointer Exception caught &quot; + ee ) ; <br>
}<br>
}<br>
}<br>
<br>
This is what i tried and it gave me a Null Pointer Exception,<br>
Am I going wrong somewhere ????<br>
Would very much appreciate a reply back on this issue.<br>
<br>
- Sun
 
Hi!<br>
<br>
You must initialize the &quot;run&quot; reference, like this:<br>
...<br>
run=Runtime.getRuntime(); <br>
run.exec ...<br>
...<br>
<br>
Good luck. Bye, Otto.
 
Actually I did exactly the same thing that u have mentioned immediately after I posted you my second question and it worked that way absolutley fine, thanks a lot for your co-operation.<br>
It helped a lot.<br>
<br>
- Sun.<br>

 
This might run the application properly, but this still requires the Java application to have knowledge of the application that a document needs to be run with, knowledge that the operating system already has if you call the Win32 ShellExecute. Is there any way to get access to ShellExecute directly instead of using the Runtime which isn't at that level?
 
Hi!<br>
<br>
This code try to run the specified file with the OS, and the OS will start the associated<br>
application with this file. I tried it on NT, but if you use WinXXXX, change the cmd to command.<br>
<br>
import java.io.*;<br>
import java.util.*;<br>
<br>
public class ShellExecute {<br>
public static void main(String[] args) {<br>
if (args.length!=1) {<br>
System.out.println(&quot;Usage: ShellExecute FileName&quot;);<br>
System.exit(0);<br>
}<br>
try {<br>
Runtime.getRuntime().exec(&quot;cmd /c &quot;+args[0]); <br>
} catch (Throwable t) { t.printStackTrace(); }<br>
}<br>
}<br>
<br>
Good luck. Bye, Otto.<br>

 
This is just to remind everyone here of the philosophy of Java, it's supposed to be cross platform. So far, from the code I've seen, you're only targetting Micro$oft OS's, don't forget about everyone else (including Linux).
 
Yes, I know that my solution is platform dependent (like M$-WORD, WinExec and ShellExecute in the original question).<br>
Thanks. Bye, Otto.<br>

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top