[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 396
  • Last Modified:

Starting a windows program using java

Is there a way to start a custm installed windows program using java?
e.g. winamp, winDVD

without knowing the install location???
0
Knightley
Asked:
Knightley
  • 3
  • 2
  • 2
  • +1
1 Solution
 
WebstormCommented:
Hi Knightley,

you can try this:

String progname="winamp.exe"; // ...
Runtime.getRuntime.exec("start "+progname);

or
Runtime.getRuntime.exec("cmd.exe /C start "+progname);

0
 
KnightleyAuthor Commented:
well, i tried this before, in actual windows console,
not seem to work.
0
 
aozarovCommented:
Using the above aproach you will need to make sure that your application (e.g. "winamp.exe") is in your system PATH
0
Technology Partners: 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!

 
aozarovCommented:
the path can be set by right clicking "My Computer" -> Properties -> Advance -> Environment variables (assuming XP or 2000)
0
 
Jim CakalicSenior Developer/ArchitectCommented:
Hi,

I think that would work, what WebStorm suggested, as long as the application you wish to start resides in a directory on the PATH environment variable. If not, you'll probably have to go spelunking in the registry. I'm going to post two packages I know of you could use to read the Windows registry. They're JNI based, of course. And then I'll post an example I had lying around using one of them that looks up the application path based on the file extension. Hope it helps.

Packages:
jRegistryKey: http://www.bayequities.com/tech/Products/jreg_key.shtml
JObjects jst: http://www.steffensiebert.de/soft/jst/

Example to use jRegistryKey:

import ca.beq.util.win32.registry.*;
import java.util.*;

public class WinRegPath {
    private static final RootKey _hkcr = RootKey.HKEY_CLASSES_ROOT;
    private String _filename;
    private String _extension;

    private WinRegPath(String filename) {
        _filename = filename;
        _extension = _filename.substring(_filename.indexOf("."));
    }

    private String getAppIdentifier() throws RegistryException {
        RegistryKey key = new RegistryKey(_hkcr, _extension);
        if (key.exists() && key.hasValues()) {
            // the data of the default value is the registered application identifier
            Iterator iter = key.values();
            while (iter.hasNext()) {
                RegistryValue value = (RegistryValue)iter.next();
                if (value.getName().length() == 0) {
                   return (String)value.getData();
                }
            }
        }
        return null;
    }

    public String getContentType() {
        try {
            RegistryKey key = new RegistryKey(_hkcr, _extension);
            if (key.exists() && key.hasValues()) {
                // the data of the "Content Type" value is the mime type
                Iterator iter = key.values();
                while (iter.hasNext()) {
                    RegistryValue value = (RegistryValue)iter.next();
                    if (value.getName().equalsIgnoreCase("Content Type")) {
                       return (String)value.getData();
                    }
                }
            }
        } catch (RegistryException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String getApplicationPath() {
        try {
           String identifier = getAppIdentifier();
            RegistryKey key = new RegistryKey(_hkcr, identifier + "\\shell\\open\\command");
            if (key.exists() && key.hasValues()) {
                // the path is the data of the default value of the shell\open\command key
                Iterator iter = key.values();
                while (iter.hasNext()) {
                    RegistryValue value = (RegistryValue)iter.next();
                    if (value.getName().length() == 0) {
                       return (String)value.getData();
                    }
                }
            }
        } catch (RegistryException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) throws Exception {
        WinRegPath win = new WinRegPath(args[0]);
        System.out.println("path=" + win.getApplicationPath());
        System.out.println("type=" + win.getContentType());
    }
}

Given the name of a file, the class will determine the application path and the mime type (aka content type in the registry) for the specified extension if registered. When working with the registry, the coding isn't nearly as difficult as deciphering the registry structure itself.


Regards,
Jim Cakalic
0
 
WebstormCommented:
>> I think that would work, what WebStorm suggested, as long as the application you wish to start resides in a directory on the PATH environment variable

It should work even if the program is not in the PATH variable (at least on Win2000 which i used to try it)
0
 
Jim CakalicSenior Developer/ArchitectCommented:
Oh, OK. Out of curiosity, how will Windows find the executable then?

Jim
0
 
aozarovCommented:
Webstorm, not everything that you install and even have in your "Program files" will work without having it in the PATH.
LIke kdiff3.exe, FileZilla.exe and others. Not sure what is the difference but I assume it has to do with the Registry
0
 
KnightleyAuthor Commented:
Webstorm, in WinXP, the approach does not work.
Maybe u tried to open a program, which is already
in the path or in the windows default directory?

As in my question: "without knowing the install location"
indicates also it could hard be in the path variable.

Anyway, thanx for all replies.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now