Double-clicking Java class file on Windows

H

hiwa

I know this really is a Windows issue but
I can't find its official documentation nowhere.

My question is: how to enable double-click
launching of a Java class which has main()
method, via Windows filetype association
mechanism?

If we can't do it, then, why it is?
 
J

Jeffrey Schwab

hiwa said:
I know this really is a Windows issue but
I can't find its official documentation nowhere.

My question is: how to enable double-click
launching of a Java class which has main()
method, via Windows filetype association
mechanism?

If we can't do it, then, why it is?

Write a script that takes the name of the class file and spawns java
with appropriate arguments. For example, on Windows XP, I've created an
"Open" action for the .class file type, defined to run the following
command:

C:\ruby\bin\rubyw.exe -w C:\files\ruby\open.rb "%1"

The ruby script open.rb is something I can also use from the command
line to open files in my own wily ways. It looks like this:

begin
f = ARGV.shift or raise "usage: open <file> [args...]"
case f
when /\\([^\\]+)\.class$/
cmd = "javaw #{$1} #{ARGV}"
system(cmd)

# <snip>handling of other file types</>

else
raise "unknown file type: #{f}"
end
rescue Exception => x
STDERR.puts x
end
 
L

Luc The Perverse

hiwa said:
I know this really is a Windows issue but
I can't find its official documentation nowhere.

My question is: how to enable double-click
launching of a Java class which has main()
method, via Windows filetype association
mechanism?

If we can't do it, then, why it is?

Jar file? Will work as long as winrar has not stolen the association.
 
D

Dale King

hiwa said:
I know this really is a Windows issue but
I can't find its official documentation nowhere.

My question is: how to enable double-click
launching of a Java class which has main()
method, via Windows filetype association
mechanism?

If we can't do it, then, why it is?

To provide a more thorough answer, what you want is to package up the
classes correctly into a jar file. With an appropriately set-up
executable jar file you can double-click it to run it in Windoze.

For more information see:

http://www.google.com/search?hl=en&q=executable+jar&btnG=Google+Search
 
H

hiwa

Dale King ã®ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸:
To provide a more thorough answer, what you want is to package up the
classes correctly into a jar file. With an appropriately set-up
executable jar file you can double-click it to run it in Windoze.

For more information see:

http://www.google.com/search?hl=en&q=executable+jar&btnG=Google+Search
Thanks. But the topic is not executable jar file.
I just have a naive question for plain Java class file and Windows
[open] action association.
Why we can't associate it with java or javaw command?
 
I

Ingo R. Homann

Hi,
To provide a more thorough answer, what you want is to package up the
classes correctly into a jar file. With an appropriately set-up
executable jar file you can double-click it to run it in Windoze.

For more information see:

http://www.google.com/search?hl=en&q=executable+jar&btnG=Google+Search

Thanks. But the topic is not executable jar file.
I just have a naive question for plain Java class file and Windows
[open] action association.
Why we can't associate it with java or javaw command?

This is because java/javaw wants the class-name without the extension
".class" (and with package-information instead of path-information).

Don't ask me *why* sun did not allow to give the class-filename as
parameter - I think that it would be a good idea, as well.

On the other hand - what's the problem to embed the classfile in a
jarfile? (You can avoid some classpath problems then, e.g....)

Ciao,
Ingo
 
D

Dale King

Ingo said:
Hi,
I know this really is a Windows issue but
I can't find its official documentation nowhere.

My question is: how to enable double-click
launching of a Java class which has main()
method, via Windows filetype association
mechanism?

If we can't do it, then, why it is?

To provide a more thorough answer, what you want is to package up the
classes correctly into a jar file. With an appropriately set-up
executable jar file you can double-click it to run it in Windoze.

For more information see:

http://www.google.com/search?hl=en&q=executable+jar&btnG=Google+Search

Thanks. But the topic is not executable jar file.
I just have a naive question for plain Java class file and Windows
[open] action association.
Why we can't associate it with java or javaw command?

This is because java/javaw wants the class-name without the extension
".class" (and with package-information instead of path-information).

Don't ask me *why* sun did not allow to give the class-filename as
parameter - I think that it would be a good idea, as well.

I can tell you why because a program is not usually a single class file.
It is a bunch of class files arranged in a tree of packages. You have to
have some form of class path. Do you expect Java to deduce your class
path to find the rest of the classes?
 
I

Ingo R. Homann

Hi,

Dale said:
I can tell you why because a program is not usually a single class file.
It is a bunch of class files arranged in a tree of packages. You have to
have some form of class path. Do you expect Java to deduce your class
path to find the rest of the classes?

I know. But in many cases, it would be enough, if java would use a
"default" classpath with the "current" directory (or the directory, in
which the classfile is placed). Indeed, that would be some kind of
deduction, but not very tricky:

Consider, you have some classes: "MainClass" in the default-package and
"packageA.subpackageX.OtherClass". Then your directories would be:

/application/bin/MainClass.class
/application/bin/packageA/subpackageX/OtherClass.class

Now, if you double-click on MainClass.class, what would be the problem
to "guess" where OtherClass is placed?

Two notes:

(1) I know that I must improve this logic, so that jar-files can be
found (as usual, "$APP/lib/*.jar")

(2) As I said: "On the other hand - what's the problem to embed the
classfile in a jarfile? (You can avoid some classpath problems then,
e.g....)"

Ciao,
Ingo

PS: Yes, I am aware that the word "subpackage" does not exist in java,
but I guess you know what I mean by this. :)
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top