Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

HOW TO OPEN or CALL AN APPLICATION IN JAVA

Posted on 2004-09-27
24
Medium Priority
?
268 Views
Last Modified: 2010-03-31
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??!
0
Comment
Question by:casper_the_ghost
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 6
  • 4
  • +2
24 Comments
 
LVL 92

Expert Comment

by:objects
ID: 12165940
try:

Runtime.exec("cmd /c start a.doc");
0
 

Author Comment

by:casper_the_ghost
ID: 12165949
is that the code for any applications i want to open in java??
0
 

Author Comment

by:casper_the_ghost
ID: 12165963
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???
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Expert Comment

by:objects
ID: 12165978
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");
0
 
LVL 12

Expert Comment

by:Giant2
ID: 12166994
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.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12167039
To open a file with the associated program (whatever that may be) in windows:

            File theFile =  new File("C:/dirA/dirB/yourFile.doc");  // or whatever other extension
            Runtime.getRuntime().exec("rundll32 SHELL32.DLL,ShellExec_RunDLL " + theFile.getAbsolutePath());
0
 

Author Comment

by:casper_the_ghost
ID: 12173133
So in the exec() method, i have to put in path.......and the whole or the exact path is ....... "exec("rundll32 SHELL32.DLL,ShellExec_RunDLL C:\Program Files\Microsoft Office\Office10\WINWORD.EXE");"   ??!!  

and for opening internet explorer is ..... "exec("rundll32 SHELL32.DLL,ShellExec_RunDLL C:\Program Files\Internet Explorer\IEXPLORE.EXE");"  ....... am i right ??!!
0
 

Author Comment

by:casper_the_ghost
ID: 12173153
could i open IE and tell it what url to open or not??!!
0
 

Author Comment

by:casper_the_ghost
ID: 12173196
do i use import java.awt.*; so i can use Runtime class and getruntime and exec method??
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12173427
>>exec("rundll32 SHELL32.DLL,ShellExec_RunDLL C:\Program Files\Microsoft Office\Office10\WINWORD.EXE");"  
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_RunDLL 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_RunDLL 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_RunDLL 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.
0
 

Author Comment

by:casper_the_ghost
ID: 12173559
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??
0
 

Author Comment

by:casper_the_ghost
ID: 12173567
why did you choose a .doc file instead of just calling WINWORD.EXE??  Would it make a difference??
0
 

Author Comment

by:casper_the_ghost
ID: 12173589
i think for the last question......i got an answer from you on that, but can elaborate on what the difference would be??  Thanks.
0
 

Author Comment

by:casper_the_ghost
ID: 12173601
Thanks for the help on the previous questions by the way you guys...........that helped a lot.
0
 
LVL 37

Accepted Solution

by:
zzynx earned 500 total points
ID: 12173954
>>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??
Yes, but you have to prefix it with "mailto:" :

        try {
           Runtime.getRuntime().exec("rundll32 SHELL32.DLL,ShellExec_RunDLL mailto:someone@aol.com");
        } catch (java.io.IOException ex) { }

>>i got an answer from you on that, but can elaborate on what the difference would be??  Thanks.
In all the previous you ask the Windows shell to start the application corresponding with a certain file.
If you want to open WinWord.exe (or whatever other program), just do:
        try {
            Runtime.getRuntime().exec("C:/Program Files/Microsoft Office/Office10/WINWORD.EXE");  // of course replace with your path
        } catch (java.io.IOException ex) { }
0
 

Author Comment

by:casper_the_ghost
ID: 12174312
oh okay...........so if i know the exact path to open the application............just do this, .exec("C:/Program Files/Microsoft Office/Office10/WINWORD.EXE");  , but if i want a file to open i need it like this, .exec("rundll32 SHELL32.DLL,ShellExec_RunDLL c:/dir1/dir2/MyFile.doc"); if i put it in that folder.

What is this..........rundll32 SHELL32.DLL,ShellExec_RunDLL.........what does that do?? or what does it mean??
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12174397
>>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
0
 
LVL 92

Expert Comment

by:objects
ID: 12174772
> What is this..........rundll32 SHELL32.DLL,ShellExec_RunDLL.........what does that do?? or what does it mean??

Thats a bit outdated I think.
The command I posted above does the same.
0
 
LVL 12

Expert Comment

by:Giant2
ID: 12177179
>.exec("rundll32 SHELL32.DLL,ShellExec_RunDLL 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"
0
 

Author Comment

by:casper_the_ghost
ID: 12182044
Thanks for all your help........
0
 
LVL 92

Expert Comment

by:objects
ID: 12184998
no worries :)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12187401
>> 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.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12187843
Thanks for accepting
0

Featured Post

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question