This content has been marked as final. Show 8 replies
PhHein, yes, I can create batch file, but I prefer creating a jar file.
So I've changed directiory to
created manifest file
echo Main-Class: ExampleProgram >manifest.txt
created my jar file
jar cvfm ExampleProgram.jar manifest.txt ExampleProgram.class
tried to test result by running my program
(It doesn't return any result in command line).
But when I run "java ExampleProgram" it does display "I'm a Simple Program"
What I'm doing wrong now?
There are two java launchers in Windows: java.exe and javaw.exe.
Normally, applications are either console applications (like yours) or GUI applications (they open frames, dialogs etc.).
java.exe is for console applications and javaw.exe is for GUI applications.
As a matter of convenience for users (not programmers!), the JRE associates jar files with GUI applications, so that they can be launched by double-click.
Console applications are for slightly more sophisticated users and they know how to use the command prompt, bat files etc.
Now, if you have a console application and launch it with javaw.exe (by double-click), you will see nothing. javaw.exe 'swalows' all the console output and rightly so.
A typical GUI application has nothing to show in the console and opening a console 'just in case' would not be a pleasant end-user experience.
As programmers, we often combine GUI and console output for troubleshooting. This is fine, but we also know that in such a case we need to open a command prompt and execute using java.exe (not javaw.exe).
java.exe lets the GUI show, but javaw.exe does not let the console show!
What I'm doing wrong now?Nothing, the Windows command prompt doesn't understand what a jar file is so can't run them as a regular "OS command"; it can only execute .exe applications that way and Java doesn't produce those, you'd need to defect to .NET if you want that. Only the Java runtime knows how to execute jars.
But as Baftos explains, you can run GUI applications directly from the windows shell by double clicking the jar. IF Windows is instructed to bind the .jar filetype to the javaw.exe executable, which the runtime installer will do for you. But command prompt stuff, you run the way you're doing now.
Compare it to an office file. You can't just type 'mydocument.doc' in a command prompt and see Word being started. But you can double click the .doc file in a shell, because Windows has been configured to associate the .doc filetype with word.exe (or whatever the name of the executable is).
baftos wrote:Somewhere deep down I was actually thinking that this was actually added to Windows between now and yonks ago :s Well, I made a valiant attempt and failed miserably.
gimbal2 wrote:Try it! At least in Windows 7, it works! In older versions, I don't know, but you can type 'start mydocument.doc'.
You can't just type 'mydocument.doc' in a command prompt and see Word being started.
baftos wrote:I just tried it.
Try it! At least in Windows 7, it works! In older versions, I don't know, but you can type 'start mydocument.doc'.
I have a newly installed Win7 Enterprise (32bit) desktop that has only the OS installed.
MS Office is not installed and there are no add-ons such as document viewer utilities.
I copied a sample MSWord document from another system on my LAN, made sure its name was in the old-style 8-dot-3 syntax, and tried to get it to work from the command prompt window. The system attempted to launch my sample document but balked at it because there were no associated applications to actually launch the file in a usable fashion.
It behaved exactly the same as if I had just double-clicked it from an Explorer window.
The graphical prompt window appeared asking to find a compatible program.
I think the earlier comment in this thread about needing an associated application is still a valid one.