Asking Windows Specific Questions

L

Luc The Perverse

If am writing a program in Java - and have a windows specific question - is
it ok to ask that here? Or is there a different group for it? Should I
x-post?

Maybe I need to start with a JNI tutorial.
 
K

Knute Johnson

Luc said:
If am writing a program in Java - and have a windows specific question - is
it ok to ask that here? Or is there a different group for it? Should I
x-post?

Maybe I need to start with a JNI tutorial.

Ask away!
 
L

Luc The Perverse

Knute Johnson said:
Ask away!

Ok! :)

Here are the questions I have had. (All of these questions relate to a
music searching program that I have no intention of being cross platform.)

1. System Integration: I would like to "launch" a file using the system's
default handler (identical functionality to double clicking a file in
explorer or putting file in run menu) I can use
Runtime.getRuntime().exec("explorer.exe " + filename) but I believe that
has high overhead, and it prompts if you want to open the file.
("D:\media\Short Vids\Star Wars Kid Matrix Parody.wmv" loads default system
handler, in this case VLC media player)

2. Configuration Management: This may not be windows specific, but it is a
problem I have with virtuall every app I write. I need to store options -
but the first piece of information I need is where the configuration file
is. Knowing the path of my JAR file should be sufficient, but I don't know
how to grab this. Some programs need to run with multiple configurations.
What is the best way to handle settings/parameters/configuration management?

3. Drag And Drop: I would like to be able to drag and drop selected items
in a JList into another application and have the same effect as though I had
dragged selected files from an instance of explorer. (I do not think I
need the specific, though useful, right dragging which gives you a menu,
because I will not be dragging to another instance of explorer)

4. Windows Messaging: Right now I am writing an application which
interacts which interacts with a windows program called winamp (A music
player). I was forced to use a third party program called CLAmp to allow
command line interaction for enqueue and playing songs. If I could send
windows messages from my program directly the only command line interaction
I would need would be to invoke winamp if it is not already running.

5. Focus Question: In order for my program to work I need to invoke another
program (winamp). As long as winamp is already running there is no
problem, but when I invoke it my application loses focus and requires an Alt
Tab or mouse click to regain focus. I looked through the available
functions for a Gain Focus, Set Focus or Get Focus and I found MANY
functions, none of which seemed to regain the window focus. Am I just
misinterpriting the names - or has Gain Focus been deliberately removed from
Java?

6. Installation Program Question: This had extensive redundancy with
question #2. Basically I am looking for an installation program that can
do all of the following:
a. Link an extension to my java application (a document extension, or in
this unusual case, the document is a "configuration")
b. Check for JRE and direct user to a location to get it if they don't
have it. (or allow a bundled JRE optionally)
c. Install Start Menu/Desktop/Quick Launch shortcuts, with hotkeys if
desired.
d. Provide uninstall functionality
e. Register DLLs or EXE files that my program interacts with
 
K

Knute Johnson

Luc said:
Ok! :)

Here are the questions I have had. (All of these questions relate to a
music searching program that I have no intention of being cross platform.)

1. System Integration: I would like to "launch" a file using the system's
default handler (identical functionality to double clicking a file in
explorer or putting file in run menu) I can use
Runtime.getRuntime().exec("explorer.exe " + filename) but I believe that
has high overhead, and it prompts if you want to open the file.
("D:\media\Short Vids\Star Wars Kid Matrix Parody.wmv" loads default system
handler, in this case VLC media player)

Haven't tried it so I don't know.
2. Configuration Management: This may not be windows specific, but it is a
problem I have with virtuall every app I write. I need to store options -
but the first piece of information I need is where the configuration file
is. Knowing the path of my JAR file should be sufficient, but I don't know
how to grab this. Some programs need to run with multiple configurations.
What is the best way to handle settings/parameters/configuration management?

Look at the System property user.home. Each user can have different
config files and it is platform independent. That way your jar archive
can live anywhere.
3. Drag And Drop: I would like to be able to drag and drop selected items
in a JList into another application and have the same effect as though I had
dragged selected files from an instance of explorer. (I do not think I
need the specific, though useful, right dragging which gives you a menu,
because I will not be dragging to another instance of explorer)

I haven't done any D&D so I can't give you an answer on that one.
4. Windows Messaging: Right now I am writing an application which
interacts which interacts with a windows program called winamp (A music
player). I was forced to use a third party program called CLAmp to allow
command line interaction for enqueue and playing songs. If I could send
windows messages from my program directly the only command line interaction
I would need would be to invoke winamp if it is not already running.

I think you will need JNI to send windows messages. I'm really curious
to know is that mechanism is going to change in the new version of
windows that is due out the end of the year.
5. Focus Question: In order for my program to work I need to invoke another
program (winamp). As long as winamp is already running there is no
problem, but when I invoke it my application loses focus and requires an Alt
Tab or mouse click to regain focus. I looked through the available
functions for a Gain Focus, Set Focus or Get Focus and I found MANY
functions, none of which seemed to regain the window focus. Am I just
misinterpriting the names - or has Gain Focus been deliberately removed from
Java?

I think you would have to do that the same way you do #4 with windows
messaging.
6. Installation Program Question: This had extensive redundancy with
question #2. Basically I am looking for an installation program that can
do all of the following:
a. Link an extension to my java application (a document extension, or in
this unusual case, the document is a "configuration")
b. Check for JRE and direct user to a location to get it if they don't
have it. (or allow a bundled JRE optionally)

You can exec java.exe and parse the command line for version. And you
can package the JRE with your app and install it if necessary.
c. Install Start Menu/Desktop/Quick Launch shortcuts, with hotkeys if
desired.
d. Provide uninstall functionality
e. Register DLLs or EXE files that my program interacts with

a - d are going to require you to write a windows program or script.
The only time I played with any of the scripting I found it was very
buggy. If you think you are going to make money off the program, I'd
just buy a commercial installer.

Sorry I wasn't much help.
 
C

Chris Smith

Luc The Perverse said:
1. System Integration: I would like to "launch" a file using the system's
default handler (identical functionality to double clicking a file in
explorer or putting file in run menu) I can use
Runtime.getRuntime().exec("explorer.exe " + filename) but I believe that
has high overhead, and it prompts if you want to open the file.
("D:\media\Short Vids\Star Wars Kid Matrix Parody.wmv" loads default system
handler, in this case VLC media player)

Try running the command "start filename". This is Windows-specific,
though, so you should check the os.name system property and perhaps
offer an alternative.
2. Configuration Management: This may not be windows specific, but it is a
problem I have with virtuall every app I write. I need to store options -
but the first piece of information I need is where the configuration file
is. Knowing the path of my JAR file should be sufficient, but I don't know
how to grab this. Some programs need to run with multiple configurations.
What is the best way to handle settings/parameters/configuration management?

Have you considered the Preferences API, in java.util.prefs. That's the
good portable Java programmer answer, and if it meets your needs it's
better than anything else you could do.

Otherwise, see this long answer:
4. Windows Messaging: Right now I am writing an application which
interacts which interacts with a windows program called winamp (A music
player). I was forced to use a third party program called CLAmp to allow
command line interaction for enqueue and playing songs. If I could send
windows messages from my program directly the only command line interaction
I would need would be to invoke winamp if it is not already running.

This would require native code, and I'm not aware of an existing API to
do it. Does winamp implement a COM automation API? If so, then JACOB
is pretty easy to use to talk to automation servers.
5. Focus Question: In order for my program to work I need to invoke another
program (winamp). As long as winamp is already running there is no
problem, but when I invoke it my application loses focus and requires an Alt
Tab or mouse click to regain focus. I looked through the available
functions for a Gain Focus, Set Focus or Get Focus and I found MANY
functions, none of which seemed to regain the window focus. Am I just
misinterpriting the names - or has Gain Focus been deliberately removed from
Java?

Try Window.toFront(). I seem to recall that it flashes the title bar,
but it whould bring the window to the font of the system, and give it
focus.

Note that Window "focus" is different from normal component focus. Each
window has its own focused component. Using requestFocus will only
change which component is focused in that window, and not which window
is active.
6. Installation Program Question: This had extensive redundancy with
question #2. Basically I am looking for an installation program that can
do all of the following:

I've used WISE to build install kits. It will do most of this, as I
imagine anything else would, too. I have heard good things about
http://www.jrsoftware.org/isinfo.php.

Two things, though.
b. Check for JRE and direct user to a location to get it if they don't
have it. (or allow a bundled JRE optionally)

Are you sure? By far the safest thing to do when distributing a Java
app is to bundle a copy of the JRE in a subdirectory of where you
install the application, and invoke that JRE by its full path. Unless
you're distributing a developer tool (in which case you would want to
let the developer specify the location of the JRE), your users shouldn't
care whether reinstalling Java in FireFox breaks your app.
e. Register DLLs or EXE files that my program interacts with

This is only necessary if they contain COM components. Do they?

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
L

Luc The Perverse

Chris Smith said:
Try running the command "start filename". This is Windows-specific,
though, so you should check the os.name system property and perhaps
offer an alternative.

I am quite busy today - I haven't had a chance to look at most of what you
said - but I will definitely save this message.

However, I wrote a simple program to test this specific suggestion (quoted
above)

--- testinvoke.java ---

public class testinvoke {
public static void main(String[] X) throws java.io.IOException{
Runtime.getRuntime().exec("start d:\\hat.bmp");
}
}

I compiled it in GEL, here is the output

E:\Program Files\Java\jdk1.5.0_06\bin\java.exe -classpath "E:\Program
Files\Java\jdk1.5.0_06\jre\lib\rt.jar;E:\Program
Files\Java\jdk1.5.0_06\lib\tools.jar;D:\Projs" testinvoke
Exception in thread "main" java.io.IOException: CreateProcess: start
d:\hat.bmp error=2
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(ProcessImpl.java:81)
at java.lang.ProcessImpl.start(ProcessImpl.java:30)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:451)
at java.lang.Runtime.exec(Runtime.java:591)
at java.lang.Runtime.exec(Runtime.java:429)
at java.lang.Runtime.exec(Runtime.java:326)
at testinvoke.main(testinvoke.java:7)
Finished executing

Noteably this is the same output as

Runtime.getRuntime().exec("InvalidFileName.exe");
 
D

Dmitry Leskov

Now as you mentioned Excelsior, do you know they also sell a JNI proxy
library that helps you call C functions and native APIs right from your
Java program? It is called xFunction and is available for Windows,
Linux and Mac OS X.

A minimalistic example:

import com.excelsior.xFunction.*;
...
/* Call Beep() from KERNEL32.DLL */
xFunction f =
new xFunction( "kernel32", "int Beep(int,int)" );
f.invoke( new Argument(1770), new Argument(100) );

Ah, and the URL is: http://www.excelsior-usa.com/xfunction.html

LDV
 
D

dimitar

start is a command of the windows shell, so what you need to do is:

Runtime.getRuntime().exec("cmd.exe -c start d:\\hat.bmp");


or even better:


// Java can use the unix style slashes on every platform
String filename="d:/hat.bmp";

String shell= System.getenv("COMSPEC");
if (shell==null) {
shell= System.getenv("SHELL");
}
if (shell==null) {
throw some exception as you would need
to handle this in the client code...
}

ProcessBuilder pb = new ProcessBuilder(shell, "-c", filename);

// we don't want to read both streams
p.redirectErrorStream(true);

Process p = pb.start();

while (p.getInputStream().read()!=-1) {
// wait for the native process to finish it might be a good
// idea to actually read the stream and ptocess the output.
};

return p.exitValue();
 
L

Luc The Perverse

dimitar said:
start is a command of the windows shell, so what you need to do is:

Runtime.getRuntime().exec("cmd.exe -c start d:\\hat.bmp");


or even better:


// Java can use the unix style slashes on every platform
String filename="d:/hat.bmp";

String shell= System.getenv("COMSPEC");
if (shell==null) {
shell= System.getenv("SHELL");
}
if (shell==null) {
throw some exception as you would need
to handle this in the client code...
}

ProcessBuilder pb = new ProcessBuilder(shell, "-c", filename);

// we don't want to read both streams
p.redirectErrorStream(true);

Process p = pb.start();

while (p.getInputStream().read()!=-1) {
// wait for the native process to finish it might be a good
// idea to actually read the stream and ptocess the output.
};

return p.exitValue();

OMG yay!

I had to change "-c" to "/c" but this is the first time I have gotten this
to work.

Oh I am so happy now thank you! I can finish my generic search program.

This is my function (basically the same as yours)

public static boolean loadWindowsDefaultHandler(String fn){
fn = '"' + fn + '"';
String shell= System.getenv("COMSPEC");
if (shell==null&&(shell= System.getenv("SHELL"))==null)
return false;
ProcessBuilder pb = new ProcessBuilder(shell, "/c", fn);
pb.redirectErrorStream(true);
try{
Process p = pb.start();
}
catch(java.io.IOException E){
return false;
}
return true;
}
 
D

dimitar

Looks good! Just 1 note: you need to consume the stream from he process,
otherwise it would block on stdout when the OS buffer is full. You can
do it in a new thread if you want.

Dimtiar
 
L

Luc The Perverse

dimitar said:
Looks good! Just 1 note: you need to consume the stream from he process,
otherwise it would block on stdout when the OS buffer is full. You can do
it in a new thread if you want.

Just with an empty while loop?

Do I need to put a sleep in the while loop?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top