casper_the_ghost
asked on
HOW TO OPEN or CALL AN APPLICATION IN JAVA
I'm using java and i created a frame and in that frame is i created a button that i labeled MS Word.......how can i write a code that when the user clicks on the button, the MS word application will open up??!
ASKER
is that the code for any applications i want to open in java??
ASKER
I don't even know what Runtime.exec() is......i mean where do you get that first of all?? And should i just put that in my ActionPerformed or ActionListener code just as it is or something else is missing in that code???
Runtime is a class, see javadoc for more details.
exec() is a method in the Runtime class to run an externap application.
you will need to get a Runtime instance using the static getRuntime() method.
Runtime.getRuntime().exec( "cmd /c start a.doc");
exec() is a method in the Runtime class to run an externap application.
you will need to get a Runtime instance using the static getRuntime() method.
Runtime.getRuntime().exec(
Code posted by objects call an external application (in Windows environment).
Logically you must insert this call where you want. If you have a button that when pressed must call this external application, you must insert this piece of code in the actionperform method linked to your button.
Logically you must insert this call where you want. If you have a button that when pressed must call this external application, you must insert this piece of code in the actionperform method linked to your button.
To open a file with the associated program (whatever that may be) in windows:
File theFile = new File("C:/dirA/dirB/yourFil e.doc"); // or whatever other extension
Runtime.getRuntime().exec( "rundll32 SHELL32.DLL,ShellExec_RunD LL " + theFile.getAbsolutePath()) ;
File theFile = new File("C:/dirA/dirB/yourFil
Runtime.getRuntime().exec(
ASKER
So in the exec() method, i have to put in path.......and the whole or the exact path is ....... "exec("rundll32 SHELL32.DLL,ShellExec_RunD LL C:\Program Files\Microsoft Office\Office10\WINWORD.EX E");" ??!!
and for opening internet explorer is ..... "exec("rundll32 SHELL32.DLL,ShellExec_RunD LL C:\Program Files\Internet Explorer\IEXPLORE.EXE");" ....... am i right ??!!
and for opening internet explorer is ..... "exec("rundll32 SHELL32.DLL,ShellExec_RunD
ASKER
could i open IE and tell it what url to open or not??!!
ASKER
do i use import java.awt.*; so i can use Runtime class and getruntime and exec method??
>>exec("rundll32 SHELL32.DLL,ShellExec_RunD LL C:\Program Files\Microsoft Office\Office10\WINWORD.EX E");"
No. You don't pass the program to open, you pass the *file* to open
e.g. You write
Runtime.getRuntime().exec( "rundll32 SHELL32.DLL,ShellExec_RunD LL c:/dir1/dir2/MyFile.doc"); // remark the forward slashes
if you have a Word file called MyFile.doc in a directory C:/dir1/dir2
>>could i open IE and tell it what url to open
Yes:
try {
Runtime.getRuntime().exec( "rundll32 SHELL32.DLL,ShellExec_RunD LL http://www.yahoo.com");
} catch (java.io.IOException ex) { }
>> do i use import java.awt.*; so i can use Runtime class and getruntime and exec method??
Not needed. The Runtime class is part of the java.lang package which is imported by default.
Try running this simple little app:
public class Demo {
public static void main(String args[]) {
try {
Runtime.getRuntime().exec( "rundll32 SHELL32.DLL,ShellExec_RunD LL http://www.yahoo.com");
} catch (java.io.IOException ex) { }
}
}
It will open your browser (IE or another one) and open Yahoo's website.
Remark: the try-catch block is needed because exec() "can" throw an IOException.
No. You don't pass the program to open, you pass the *file* to open
e.g. You write
Runtime.getRuntime().exec(
if you have a Word file called MyFile.doc in a directory C:/dir1/dir2
>>could i open IE and tell it what url to open
Yes:
try {
Runtime.getRuntime().exec(
} catch (java.io.IOException ex) { }
>> do i use import java.awt.*; so i can use Runtime class and getruntime and exec method??
Not needed. The Runtime class is part of the java.lang package which is imported by default.
Try running this simple little app:
public class Demo {
public static void main(String args[]) {
try {
Runtime.getRuntime().exec(
} catch (java.io.IOException ex) { }
}
}
It will open your browser (IE or another one) and open Yahoo's website.
Remark: the try-catch block is needed because exec() "can" throw an IOException.
ASKER
so it depends what kind of browser the user has........but if i put in an e-mail like caspertgh0@aol.com instead of yahoo.com.....it will open the default e-mail connection of the user like AOL application or MS Outlook depending on which ones the user chooses as his default e-mail handler??
ASKER
why did you choose a .doc file instead of just calling WINWORD.EXE?? Would it make a difference??
ASKER
i think for the last question......i got an answer from you on that, but can elaborate on what the difference would be?? Thanks.
ASKER
Thanks for the help on the previous questions by the way you guys...........that helped a lot.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
oh okay...........so if i know the exact path to open the application............jus t do this, .exec("C:/Program Files/Microsoft Office/Office10/WINWORD.EX E"); , but if i want a file to open i need it like this, .exec("rundll32 SHELL32.DLL,ShellExec_RunD LL c:/dir1/dir2/MyFile.doc"); if i put it in that folder.
What is this..........rundll32 SHELL32.DLL,ShellExec_RunD LL........ .what does that do?? or what does it mean??
What is this..........rundll32 SHELL32.DLL,ShellExec_RunD
>>what does that do?? or what does it mean??
As I told before:
>> ... you ask Window's shell to start the application corresponding with a certain file.
It's the same as double-clicking a file in the Windows Explorer
As I told before:
>> ... you ask Window's shell to start the application corresponding with a certain file.
It's the same as double-clicking a file in the Windows Explorer
> What is this..........rundll32 SHELL32.DLL,ShellExec_RunD LL........ .what does that do?? or what does it mean??
Thats a bit outdated I think.
The command I posted above does the same.
Thats a bit outdated I think.
The command I posted above does the same.
>.exec("rundll32 SHELL32.DLL,ShellExec_RunD LL c:/dir1/dir2/MyFile.doc");
If you know it is a doc file then you can open it executing directly "winword c:/dir1/.../MyFile.doc"
If you know it is a doc file then you can open it executing directly "winword c:/dir1/.../MyFile.doc"
ASKER
Thanks for all your help........
no worries :)
>> Thanks for all your help........
If you have no more questions left about this topic, please kindly close this Q by accepting one or more comments as accepted/assisted answer.
If you have no more questions left about this topic, please kindly close this Q by accepting one or more comments as accepted/assisted answer.
Thanks for accepting
Runtime.exec("cmd /c start a.doc");