accessing the registry

Discussion in 'Java' started by soni29, Jul 16, 2003.

  1. soni29

    soni29 Guest

    hi,
    i know that since the registry is only part of windows there probably
    isn't a class in the sdk that will allow me to read from it. but does
    anyone know of any free third party classes i can use? i've searched
    and read about using jni, but don't know much about c or how to get
    into the registry using c++. also on some forums someone mentioned
    using the preferences class in the sdk, but i'm not 100% about that.
    what i need to do is find out if the user has ms office installed on
    their pc, if so where, i need to be able to launch office exe from my
    app, we were building something we thought was going to run on linux
    and windows, but all of a sudden my manager came and said forget it
    now only windows, and he wants to try to add the launching ms office
    suite into it. any ideas?

    Thank you.
     
    soni29, Jul 16, 2003
    #1
    1. Advertising

  2. soni29

    Tim Tyler Guest

    soni29 <> wrote:

    : i know that since the registry is only part of windows there probably
    : isn't a class in the sdk that will allow me to read from it. but does
    : anyone know of any free third party classes i can use?
    : i've searched and read about using jni, but don't know much about c or
    : how to get into the registry using c++. also on some forums someone
    : mentioned using the preferences class in the sdk, but i'm not 100%
    : about that.

    You can read from the registry in Java using java.util.prefs.

    However you can only read out things you've put in.

    The location of the MS office suite won't fit in that category.

    Have you tried "exec"ing an appropriate file type?
    --
    __________
    |im |yler http://timtyler.org/
     
    Tim Tyler, Jul 16, 2003
    #2
    1. Advertising

  3. soni29

    Roedy Green Guest

    On Wed, 16 Jul 2003 19:18:19 +0100, "Andy Flowers"
    <> wrote or quoted :

    >> : i know that since the registry is only part of windows there probably
    >> : isn't a class in the sdk that will allow me to read from it. but does
    >> : anyone know of any free third party classes i can use?
    >> : i've searched and read about using jni, but don't know much about c or
    >> : how to get into the registry using c++. also on some forums someone
    >> : mentioned using the preferences class in the sdk, but i'm not 100%
    >> : about that.


    you need a tiny bit of C++ and JNI glue to hook into the registry
    manipulating api.

    I would create it for you for $10 US.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Jul 16, 2003
    #3
  4. soni29

    Grant Wagner Guest

    soni29 wrote:

    > hi,
    > i know that since the registry is only part of windows there probably
    > isn't a class in the sdk that will allow me to read from it. but does
    > anyone know of any free third party classes i can use? i've searched
    > and read about using jni, but don't know much about c or how to get
    > into the registry using c++. also on some forums someone mentioned
    > using the preferences class in the sdk, but i'm not 100% about that.
    > what i need to do is find out if the user has ms office installed on
    > their pc, if so where, i need to be able to launch office exe from my
    > app, we were building something we thought was going to run on linux
    > and windows, but all of a sudden my manager came and said forget it
    > now only windows, and he wants to try to add the launching ms office
    > suite into it. any ideas?
    >
    > Thank you.


    Why do you need to read the Registry in order to launch MS Office
    applications?

    Runtime rt = Runtime.getRuntime();
    Process p = rt.exec("cmd /c start MyWordDocument.doc");

    If you want the path to a Microsoft Office program that handles a
    particular document type, then you can retrieve that as well:

    import java.io.*;

    public class FileHandler {
    public static void main(String[] s) {
    String extension = "";
    if (s.length == 1) {
    extension = s[0];
    try {
    String output;
    Runtime rt = Runtime.getRuntime();
    Process p = rt.exec("cmd.exe /c " +
    "for /f \"tokens=2 delims==\" %a " +
    "in ('assoc ." + extension + "') " +
    "do @for /f \"tokens=2 delims==\" %b " +
    "in ('ftype %a') do @echo %b");

    InputStreamReader isr = new
    InputStreamReader(p.getInputStream());
    BufferedReader br = new BufferedReader(isr);
    try {
    while ((output = br.readLine()) != null) {
    System.out.println(output);
    }
    } catch (IOException e) {
    System.err.println(e);
    }
    } catch (IOException e1) {
    System.err.println(e1);
    }
    } else {
    System.out.println("Usage: FileHandler [extension]");
    }
    }
    }

    "java FileHandler doc" reports "C:\Program Files\Microsoft
    Office\Office\Winword.exe" /n on my machine.

    Note that the above will only work on Windows 2000/XP.

    --
    | Grant Wagner <>
     
    Grant Wagner, Jul 16, 2003
    #4
  5. (soni29) wrote in message news:<>...
    > hi,
    > i know that since the registry is only part of windows there probably
    > isn't a class in the sdk that will allow me to read from it. but does
    > anyone know of any free third party classes i can use?


    There is a fully-functional registry support available in WinPack for
    JNIWrapper (http://www.jniwrapper.com/winpack_features.jsp#registry).
    Check the WinPack Demo to see the abilities.

    Hope this helps.
    Good Luck!
    EToporov
     
    Eugene Toporov, Jul 17, 2003
    #5
  6. soni29

    soni29 Guest

    Got it, thank you.

    Grant Wagner <> wrote in message news:<>...
    > soni29 wrote:
    >
    > > hi,
    > > i know that since the registry is only part of windows there probably
    > > isn't a class in the sdk that will allow me to read from it. but does
    > > anyone know of any free third party classes i can use? i've searched
    > > and read about using jni, but don't know much about c or how to get
    > > into the registry using c++. also on some forums someone mentioned
    > > using the preferences class in the sdk, but i'm not 100% about that.
    > > what i need to do is find out if the user has ms office installed on
    > > their pc, if so where, i need to be able to launch office exe from my
    > > app, we were building something we thought was going to run on linux
    > > and windows, but all of a sudden my manager came and said forget it
    > > now only windows, and he wants to try to add the launching ms office
    > > suite into it. any ideas?
    > >
    > > Thank you.

    >
    > Why do you need to read the Registry in order to launch MS Office
    > applications?
    >
    > Runtime rt = Runtime.getRuntime();
    > Process p = rt.exec("cmd /c start MyWordDocument.doc");
    >
    > If you want the path to a Microsoft Office program that handles a
    > particular document type, then you can retrieve that as well:
    >
    > import java.io.*;
    >
    > public class FileHandler {
    > public static void main(String[] s) {
    > String extension = "";
    > if (s.length == 1) {
    > extension = s[0];
    > try {
    > String output;
    > Runtime rt = Runtime.getRuntime();
    > Process p = rt.exec("cmd.exe /c " +
    > "for /f \"tokens=2 delims==\" %a " +
    > "in ('assoc ." + extension + "') " +
    > "do @for /f \"tokens=2 delims==\" %b " +
    > "in ('ftype %a') do @echo %b");
    >
    > InputStreamReader isr = new
    > InputStreamReader(p.getInputStream());
    > BufferedReader br = new BufferedReader(isr);
    > try {
    > while ((output = br.readLine()) != null) {
    > System.out.println(output);
    > }
    > } catch (IOException e) {
    > System.err.println(e);
    > }
    > } catch (IOException e1) {
    > System.err.println(e1);
    > }
    > } else {
    > System.out.println("Usage: FileHandler [extension]");
    > }
    > }
    > }
    >
    > "java FileHandler doc" reports "C:\Program Files\Microsoft
    > Office\Office\Winword.exe" /n on my machine.
    >
    > Note that the above will only work on Windows 2000/XP.
     
    soni29, Jul 17, 2003
    #6
  7. soni29

    soni29 Guest

    Thank you again for the response and solution, if possible could you
    tell me what this means in your code:

    Process p = rt.exec("cmd.exe /c " +
    "for /f \"tokens=2 delims==\" %a " +
    "in ('assoc ." + extension + "') " +
    "do @for /f \"tokens=2 delims==\" %b " +
    "in ('ftype %a') do @echo %b");

    i know the Process class, and cmd.exe just trying to see the for
    /f.... where did that come from?

    Thank you.

    (soni29) wrote in message news:<>...
    > Got it, thank you.
    >
    > Grant Wagner <> wrote in message news:<>...
    > > soni29 wrote:
    > >
    > > > hi,
    > > > i know that since the registry is only part of windows there probably
    > > > isn't a class in the sdk that will allow me to read from it. but does
    > > > anyone know of any free third party classes i can use? i've searched
    > > > and read about using jni, but don't know much about c or how to get
    > > > into the registry using c++. also on some forums someone mentioned
    > > > using the preferences class in the sdk, but i'm not 100% about that.
    > > > what i need to do is find out if the user has ms office installed on
    > > > their pc, if so where, i need to be able to launch office exe from my
    > > > app, we were building something we thought was going to run on linux
    > > > and windows, but all of a sudden my manager came and said forget it
    > > > now only windows, and he wants to try to add the launching ms office
    > > > suite into it. any ideas?
    > > >
    > > > Thank you.

    > >
    > > Why do you need to read the Registry in order to launch MS Office
    > > applications?
    > >
    > > Runtime rt = Runtime.getRuntime();
    > > Process p = rt.exec("cmd /c start MyWordDocument.doc");
    > >
    > > If you want the path to a Microsoft Office program that handles a
    > > particular document type, then you can retrieve that as well:
    > >
    > > import java.io.*;
    > >
    > > public class FileHandler {
    > > public static void main(String[] s) {
    > > String extension = "";
    > > if (s.length == 1) {
    > > extension = s[0];
    > > try {
    > > String output;
    > > Runtime rt = Runtime.getRuntime();
    > > Process p = rt.exec("cmd.exe /c " +
    > > "for /f \"tokens=2 delims==\" %a " +
    > > "in ('assoc ." + extension + "') " +
    > > "do @for /f \"tokens=2 delims==\" %b " +
    > > "in ('ftype %a') do @echo %b");
    > >
    > > InputStreamReader isr = new
    > > InputStreamReader(p.getInputStream());
    > > BufferedReader br = new BufferedReader(isr);
    > > try {
    > > while ((output = br.readLine()) != null) {
    > > System.out.println(output);
    > > }
    > > } catch (IOException e) {
    > > System.err.println(e);
    > > }
    > > } catch (IOException e1) {
    > > System.err.println(e1);
    > > }
    > > } else {
    > > System.out.println("Usage: FileHandler [extension]");
    > > }
    > > }
    > > }
    > >
    > > "java FileHandler doc" reports "C:\Program Files\Microsoft
    > > Office\Office\Winword.exe" /n on my machine.
    > >
    > > Note that the above will only work on Windows 2000/XP.
     
    soni29, Jul 17, 2003
    #7
  8. soni29

    Daniel Hagen Guest

    (soni29) wrote in message news:<>...
    > hi,
    > i know that since the registry is only part of windows there probably
    > isn't a class in the sdk that will allow me to read from it. but does
    > anyone know of any free third party classes i can use? i've searched


    Depends on the VM you are using. With the Microsoft VM, you can use
    the Registry Classes from com.ms.wfc.app for reading from and writing
    to the registry.

    Regards

    Daniel Hagen
     
    Daniel Hagen, Jul 18, 2003
    #8
  9. soni29

    Grant Wagner Guest

    cmd.exe /c just executes the command that follows it in a Windows NT command prompt, the rest is:

    for /f "tokens=2 delims==" %a in ('assoc .doc') do @for /f "tokens=2 delims==" %b in ('ftype %a') do @echo %b

    - for /f executes the "for" command with the ability to retrieve formatting
    - "tokens=2 delims==" says, split the retrieved result at "=" and return the 2nd token
    - %a is the variable to place the result in
    - 'assoc .doc' is executed and in the case of .doc, returns ".doc=Word.Document.8"
    - "Word.Document.8" (token 2) is placed in %a
    - the "do" is preformed
    - @for /f executes "for" with formatting options again, the @ says to suppress echoing the command
    - 'ftype %a' (%a now contains Word.Document.8) is executed, and the stuff after the = is placed in %b
    - %b is then @echoed (again, @ suppresses the actual command), so Java can retrieve the result

    All the above basically executes:

    assoc .doc
    returns ".doc=Word.Document.8"
    ftype Word.Document.8
    returns "Word.Document.8="C:\Program Files\Microsoft Office\Office\Winword.exe" /n"

    I came up with this solution when I needed access to the "help application" for a particular file type in some
    batch files, the nice thing is that if you have a language that can call system commands (such as Java), it
    works equally well there, all without requiring any actual Registry access.

    I also wanted to demonstrate the power of the "for" command in Windows NT.

    soni29 wrote:

    > Thank you again for the response and solution, if possible could you
    > tell me what this means in your code:
    >
    > Process p = rt.exec("cmd.exe /c " +
    > "for /f \"tokens=2 delims==\" %a " +
    > "in ('assoc ." + extension + "') " +
    > "do @for /f \"tokens=2 delims==\" %b " +
    > "in ('ftype %a') do @echo %b");
    >
    > i know the Process class, and cmd.exe just trying to see the for
    > /f.... where did that come from?
    >
    > Thank you.
    >
    > (soni29) wrote in message news:<>...
    > > Got it, thank you.
    > >
    > > Grant Wagner <> wrote in message news:<>...
    > > > soni29 wrote:
    > > >
    > > > > hi,
    > > > > i know that since the registry is only part of windows there probably
    > > > > isn't a class in the sdk that will allow me to read from it. but does
    > > > > anyone know of any free third party classes i can use? i've searched
    > > > > and read about using jni, but don't know much about c or how to get
    > > > > into the registry using c++. also on some forums someone mentioned
    > > > > using the preferences class in the sdk, but i'm not 100% about that.
    > > > > what i need to do is find out if the user has ms office installed on
    > > > > their pc, if so where, i need to be able to launch office exe from my
    > > > > app, we were building something we thought was going to run on linux
    > > > > and windows, but all of a sudden my manager came and said forget it
    > > > > now only windows, and he wants to try to add the launching ms office
    > > > > suite into it. any ideas?
    > > > >
    > > > > Thank you.
    > > >
    > > > Why do you need to read the Registry in order to launch MS Office
    > > > applications?
    > > >
    > > > Runtime rt = Runtime.getRuntime();
    > > > Process p = rt.exec("cmd /c start MyWordDocument.doc");
    > > >
    > > > If you want the path to a Microsoft Office program that handles a
    > > > particular document type, then you can retrieve that as well:
    > > >
    > > > import java.io.*;
    > > >
    > > > public class FileHandler {
    > > > public static void main(String[] s) {
    > > > String extension = "";
    > > > if (s.length == 1) {
    > > > extension = s[0];
    > > > try {
    > > > String output;
    > > > Runtime rt = Runtime.getRuntime();
    > > > Process p = rt.exec("cmd.exe /c " +
    > > > "for /f \"tokens=2 delims==\" %a " +
    > > > "in ('assoc ." + extension + "') " +
    > > > "do @for /f \"tokens=2 delims==\" %b " +
    > > > "in ('ftype %a') do @echo %b");
    > > >
    > > > InputStreamReader isr = new
    > > > InputStreamReader(p.getInputStream());
    > > > BufferedReader br = new BufferedReader(isr);
    > > > try {
    > > > while ((output = br.readLine()) != null) {
    > > > System.out.println(output);
    > > > }
    > > > } catch (IOException e) {
    > > > System.err.println(e);
    > > > }
    > > > } catch (IOException e1) {
    > > > System.err.println(e1);
    > > > }
    > > > } else {
    > > > System.out.println("Usage: FileHandler [extension]");
    > > > }
    > > > }
    > > > }
    > > >
    > > > "java FileHandler doc" reports "C:\Program Files\Microsoft
    > > > Office\Office\Winword.exe" /n on my machine.
    > > >
    > > > Note that the above will only work on Windows 2000/XP.


    --
    | Grant Wagner <>
     
    Grant Wagner, Jul 18, 2003
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Daniel Bass
    Replies:
    2
    Views:
    448
    Michael Pearson
    Jan 28, 2004
  2. HK
    Replies:
    1
    Views:
    3,716
    Cowboy \(Gregory A. Beamer\)
    Apr 1, 2004
  3. Replies:
    0
    Views:
    783
  4. Leny
    Replies:
    3
    Views:
    16,985
    Daniel
    Feb 1, 2005
  5. Collin Miller
    Replies:
    10
    Views:
    682
    Roger Pack
    Jul 9, 2010
Loading...

Share This Page