This discussion is archived
8 Replies Latest reply: Apr 26, 2013 7:48 AM by rukbat RSS

run java program

marco Newbie
Currently Being Moderated
Hi all,

I'm very new to java, tried to write my first program and it works.

I typed in notepad:

//A Very Simple Example
class ExampleProgram {
public static void main(String[] args) {
System.out.println("I'm a Simple Program");
}
}

saved it as ExampleProgram.java

and then compiled it using

javac ExampleProgram.java

Now I can run it:

java ExampleProgram

and it displays text "I'm a Simple Program".

Now the question is - how do I run my program without typing "java [program name]" ? I mean, I'd like rather to run it using some kind of ExampleProgram.exe but without typing "java" before program name. Is it possible at all?

Edited by: marco on 25/4/2013 6:46
  • 1. Re: run java program
    gimbal2 Guru
    Currently Being Moderated
    Yes, but that is more advanced material. Keep studying, you will get to that eventually. If you're really curious, do a google for "java executable jar".
  • 2. Re: run java program
    PhHein Guru Moderator
    Currently Being Moderated
    You could create a batch file that executes the java ExampleProgram call and just double click that. Or you create a runnable jar file.
  • 3. Re: run java program
    marco Newbie
    Currently Being Moderated
    PhHein, yes, I can create batch file, but I prefer creating a jar file.

    So I've changed directiory to

    C:\oracle\Middleware\jdk160_24\bin

    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

    ExampleProgram.jar

    (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?
  • 4. Re: run java program
    baftos Expert
    Currently Being Moderated
    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!
  • 5. Re: run java program
    gimbal2 Guru
    Currently Being Moderated
    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).
  • 6. Re: run java program
    baftos Expert
    Currently Being Moderated
    gimbal2 wrote:
    You can't just type 'mydocument.doc' in a command prompt and see Word being started.
    Try it! At least in Windows 7, it works! In older versions, I don't know, but you can type 'start mydocument.doc'.
  • 7. Re: run java program
    gimbal2 Guru
    Currently Being Moderated
    baftos wrote:
    gimbal2 wrote:
    You can't just type 'mydocument.doc' in a command prompt and see Word being started.
    Try it! At least in Windows 7, it works! In older versions, I don't know, but you can type 'start mydocument.doc'.
    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.
  • 8. Re: run java program
    rukbat Guru Moderator
    Currently Being Moderated
    baftos wrote:
    Try it! At least in Windows 7, it works! In older versions, I don't know, but you can type 'start mydocument.doc'.
    I just tried it.
    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.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points