Self-executing JAR

Discussion in 'Java' started by Tim Slattery, Apr 16, 2012.

  1. Tim Slattery

    Tim Slattery Guest

    I must be doing something stupid.

    I have a very simple command-line program in Eclipse, just a
    System.out.println("Hello World"). It runs just fine in the IDE. So I
    use File|Export|Export, and select "Runnable JAR file". A JAR is
    created.

    And it doesn't work. I can double-click on it, or I can call it from a
    command line. I get nothing. No "Hello world", no error message, no
    nothing. What have I missed?

    --
    Tim Slattery
     
    Tim Slattery, Apr 16, 2012
    #1
    1. Advertising

  2. Tim Slattery

    Daniel Pitts Guest

    On 4/16/12 11:46 AM, Tim Slattery wrote:
    > I must be doing something stupid.
    >
    > I have a very simple command-line program in Eclipse, just a
    > System.out.println("Hello World"). It runs just fine in the IDE. So I
    > use File|Export|Export, and select "Runnable JAR file". A JAR is
    > created.
    >
    > And it doesn't work. I can double-click on it, or I can call it from a
    > command line. I get nothing. No "Hello world", no error message, no
    > nothing. What have I missed?

    You have missed the SSCCE.

    It could be that you need to set a "main" class in your build
    configuration somewhere. I don't know much about Eclipse.

    I do know that there is a standard file in META-INF that points to the
    class that should be run to start the runnable jar. Google may tell you
    more.
     
    Daniel Pitts, Apr 16, 2012
    #2
    1. Advertising

  3. Tim Slattery

    markspace Guest

    On 4/16/2012 11:46 AM, Tim Slattery wrote:
    > I must be doing something stupid.
    >
    > I have a very simple command-line program in Eclipse, just a
    > System.out.println("Hello World"). It runs just fine in the IDE. So I
    > use File|Export|Export, and select "Runnable JAR file". A JAR is
    > created.
    >
    > And it doesn't work. I can double-click on it, or I can call it from a
    > command line. I get nothing. No "Hello world", no error message, no
    > nothing. What have I missed?
    >



    How do you "call it from the command line?" Show us the command line
    and its output. You'll need a properly set PATH variable to the Java
    runtime for this to work of course. Have you done that?

    What OS do you have? You man have to set a file association from
    Windows so you can double click on a Jar file.

    Etc. Needs more info....
     
    markspace, Apr 16, 2012
    #3
  4. Tim Slattery

    Tim Slattery Guest

    Tim Slattery <> wrote:

    >I must be doing something stupid.
    >
    >I have a very simple command-line program in Eclipse, just a
    >System.out.println("Hello World"). It runs just fine in the IDE. So I
    >use File|Export|Export, and select "Runnable JAR file". A JAR is
    >created.
    >
    >And it doesn't work. I can double-click on it, or I can call it from a
    >command line. I get nothing. No "Hello world", no error message, no
    >nothing. What have I missed?


    More info:
    Here's the class:


    public class Main {
    public static void main(String[] args)
    {
    System.out.println("Here I am!");
    try
    {
    SimpleDateFormat sdf = new
    SimpleDateFormat("MM/dd/yyyy");
    sdf.setLenient(false);
    Date mydate = sdf.parse("2/29/1900");
    System.out.println("Good date: " + mydate.toString());
    }
    catch (ParseException ex)
    {
    System.out.println("ParseException: " +
    ex.getMessage());
    }

    }

    /* (non-Java-doc)
    * @see java.lang.Object#Object()
    */
    public Main() {
    super();
    }

    I export "Main.jar". To invoke from the command line, I type
    "Main.jar". Nothing but a command prompt. I have jedit installed. If I
    go to its directory and type "jedit.jar", it jumps right up. Therefore
    I assume that the JRE can be found.

    --
    Tim Slattery
     
    Tim Slattery, Apr 16, 2012
    #4
  5. Tim Slattery

    Lew Guest

    Tim Slattery wrote:
    >> I have a very simple command-line program in Eclipse, just a
    >> System.out.println("Hello World"). It runs just fine in the IDE. So I
    >> use File|Export|Export, and select "Runnable JAR file". A JAR is
    >> created.
    >>
    >> And it doesn't work. I can double-click on it, or I can call it from a
    >> command line. I get nothing. No "Hello world", no error message, no
    >> nothing. What have I missed?

    >
    > More info:
    > Here's the class:
    >
    >
    > public class Main {
    > public static void main(String[] args)
    > {
    > System.out.println("Here I am!");
    > try
    > {
    > SimpleDateFormat sdf = new
    > SimpleDateFormat("MM/dd/yyyy");
    > sdf.setLenient(false);
    > Date mydate = sdf.parse("2/29/1900");
    > System.out.println("Good date: " + mydate.toString());
    > }
    > catch (ParseException ex)
    > {
    > System.out.println("ParseException: " +
    > ex.getMessage());
    > }
    >
    > }
    >
    > /* (non-Java-doc)
    > * @see java.lang.Object#Object()
    > */
    > public Main() {
    > super();
    > }
    >
    > I export "Main.jar". To invoke from the command line, I type
    > "Main.jar". Nothing but a command prompt. I have jedit installed. If I
    > go to its directory and type "jedit.jar", it jumps right up. Therefore
    > I assume that the JRE can be found.


    "Main.jar" is not an executable, so it won't execute unless you give it one:

    java -jar Main.jar

    Show us the manifest, please?

    Did you tell Windows to associate JAR files with the "java -jar" command, as "markspace" suggested?

    What happens if you have a Word doc, say "foo.doc", and you type "foo.doc" at the command line?

    --
    Lew
     
    Lew, Apr 16, 2012
    #5
  6. On 4/16/2012 1:34 PM, Tim Slattery wrote:
    > Tim Slattery<> wrote:
    >
    >> I must be doing something stupid.
    >>
    >> I have a very simple command-line program in Eclipse, just a
    >> System.out.println("Hello World"). It runs just fine in the IDE. So I
    >> use File|Export|Export, and select "Runnable JAR file". A JAR is
    >> created.
    >>
    >> And it doesn't work. I can double-click on it, or I can call it from a
    >> command line. I get nothing. No "Hello world", no error message, no
    >> nothing. What have I missed?

    >
    > More info:
    > Here's the class:
    >
    >
    > public class Main {
    > public static void main(String[] args)
    > {
    > System.out.println("Here I am!");
    > try
    > {
    > SimpleDateFormat sdf = new
    > SimpleDateFormat("MM/dd/yyyy");
    > sdf.setLenient(false);
    > Date mydate = sdf.parse("2/29/1900");
    > System.out.println("Good date: " + mydate.toString());
    > }
    > catch (ParseException ex)
    > {
    > System.out.println("ParseException: " +
    > ex.getMessage());
    > }
    >
    > }
    >
    > /* (non-Java-doc)
    > * @see java.lang.Object#Object()
    > */
    > public Main() {
    > super();
    > }
    >
    > I export "Main.jar". To invoke from the command line, I type
    > "Main.jar". Nothing but a command prompt. I have jedit installed. If I
    > go to its directory and type "jedit.jar", it jumps right up. Therefore
    > I assume that the JRE can be found.
    >


    You are missing something here, that can't compile as it is. Try
    compiling from the command line.

    javac Main.java

    and if that produces no errors, then run it with

    java Main

    --

    Knute Johnson
     
    Knute Johnson, Apr 16, 2012
    #6
  7. Tim Slattery

    markspace Guest

    On 4/16/2012 1:34 PM, Tim Slattery wrote:

    > I export "Main.jar". To invoke from the command line, I type
    > "Main.jar". Nothing but a command prompt. I have jedit installed. If I
    > go to its directory and type "jedit.jar", it jumps right up. Therefore
    > I assume that the JRE can be found.



    OK, I tried it and I get the same behavior. BTW, this is what I mean
    when I ask "show us the command line":


    C:\Users\Brenden\Dev\proj\hw\dist>hw.jar

    C:\Users\Brenden\Dev\proj\hw\dist>java -jar hw.jar
    Hello world!

    C:\Users\Brenden\Dev\proj\hw\dist>

    That's cut-and-paste directly from my window on my computer. It's easy
    to do, saves typing, and most importantly avoids typos when you're
    trying to ask a technical question. Learn how to do it (use Google).

    So I tried it with a new GUI app I made (just shows a dialog box with
    "Hello World!") and that displays the dialog, but there's still no
    console output. So I'm sure I have the file association set, and
    further I'm still seeing the same behavior as you. I'm guessing this is
    Microsoft interacting poorly with other vendors' products, and leave it
    at that. File a bug report if you believe that will help. Meanwhile,
    just use the "java -jar" command like I did above.

    For others who might like to test this, here is the GUI test version:


    package hwgui;

    import javax.swing.JOptionPane;
    import javax.swing.SwingUtilities;

    /**
    *
    * @author Brenden
    */
    public class Hwgui
    {
    public static void main( String[] args )
    {
    SwingUtilities.invokeLater( new Runnable()
    {
    public void run()
    {
    JOptionPane.showMessageDialog( null, "Hello World!");
    }
    } );
    System.out.println( "Hello World!" );
    }
    }
     
    markspace, Apr 16, 2012
    #7
  8. Tim Slattery

    markspace Guest

    On 4/16/2012 2:51 PM, markspace wrote:

    > System.out.println( "Hello World!" );

    System.console().printf( "console-Hello World!" );


    I added a second line after this to print out to the system console as
    shown, and still no output. Now I'm somewhat aggrieved.
     
    markspace, Apr 16, 2012
    #8
  9. Tim Slattery

    Lew Guest

    Tim Slattery wrote:
    > More info:
    > Here's the class:
    >
    >
    > public class Main {
    > public static void main(String[] args)
    > {
    > System.out.println("Here I am!");
    > try
    > {
    > SimpleDateFormat sdf = new
    > SimpleDateFormat("MM/dd/yyyy");
    > sdf.setLenient(false);
    > Date mydate = sdf.parse("2/29/1900");


    There was no such date, you know.

    > System.out.println("Good date: " + mydate.toString());
    > }
    > catch (ParseException ex)
    > {
    > System.out.println("ParseException: " +
    > ex.getMessage());
    > }
    >
    > }
    >
    > /* (non-Java-doc)
    > * @see java.lang.Object#Object()
    > */
    > public Main() {
    > super();
    > }


    You don't need to specify that constructor, as it will be generated for you, and you certainly never need to call the no-argument 'super()'.

    You're missing a closing curly brace.

    > I export "Main.jar". To invoke from the command line, I type
    > "Main.jar". Nothing but a command prompt. I have jedit installed. If I
    > go to its directory and type "jedit.jar", it jumps right up. Therefore
    > I assume that the JRE can be found.


    What happens if you use the "java -jar" command?

    --
    Lew
     
    Lew, Apr 16, 2012
    #9
  10. In article
    <27365251.1727.1334611607107.JavaMail.geo-discussion-forums@pbcto7>,
    Lew <> wrote:

    > "Main.jar" is not an executable, so it won't execute unless you give it one:
    >
    > java -jar Main.jar
    >
    > Show us the manifest, please?


    Tim: If you're not sure about the manifest, this utility will display
    your JAR's manifest, as built:

    <http://sites.google.com/site/drjohnbmatthews/manifesto>

    --
    John B. Matthews
    trashgod at gmail dot com
    <http://sites.google.com/site/drjohnbmatthews>
     
    John B. Matthews, Apr 17, 2012
    #10
  11. markspace <-@> wrote:
    > C:\Users\Brenden\Dev\proj\hw\dist>hw.jar
    >
    > C:\Users\Brenden\Dev\proj\hw\dist>java -jar hw.jar
    > Hello world!
    >
    > C:\Users\Brenden\Dev\proj\hw\dist>


    I don't have a Windows-PC at hand, but I'd bet that:

    < C:\Users\Brenden\Dev\proj\hw\dist>javaw -jar hw.jar
    <
    < C:\Users\Brenden\Dev\proj\hw\dist>

    and that "javaw.exe" is registered as the default-handler for .jar

    (mind the extra "w" in the javaw command)

    PS:
    If the jarfile is just for your own use and you need it as a console-
    app, then you could rename it to hw.blah, and register java -jar as
    handler for .blah files. Once you start making GUI-apps, you just leave
    them as hw.jar (i.e. no longer rename them to hw.blah)
     
    Andreas Leitgeb, Apr 17, 2012
    #11
  12. Tim Slattery

    Roedy Green Guest

    On Mon, 16 Apr 2012 14:46:47 -0400, Tim Slattery <>
    wrote, quoted or indirectly quoted someone who said :

    >And it doesn't work. I can double-click on it, or I can call it from a
    >command line. I get nothing. No "Hello world", no error message, no
    >nothing. What have I missed?


    see http://mindprod.com/jgloss/jar.html

    Associate java.exe not javaw.exe or you will not see any output.
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    When you were a child, if you did your own experiment
    to see if it was better to put to cocoa into your cup first
    or the hot milk first, then you likely have the programmer gene..
     
    Roedy Green, Apr 17, 2012
    #12
  13. Tim Slattery

    Tim Slattery Guest

    Lew <> wrote:


    >> Date mydate = sdf.parse("2/29/1900");

    >
    >There was no such date, you know.


    I know. This started as a little test to see how SimpleDateFormat
    would handle a non-existent date. I don't have a lot of experience
    with Eclipse, so I then tried to see if I could run it outside of the
    IDE.

    --
    Tim Slattery
     
    Tim Slattery, Apr 17, 2012
    #13
  14. Tim Slattery

    markspace Guest

    On 4/17/2012 1:31 AM, Andreas Leitgeb wrote:

    > and that "javaw.exe" is registered as the default-handler for .jar



    I think you've got it. I just tried javaw on the command line and it
    works exactly the same as the default handler. I can't see what program
    Windows is using as the default (it only shows me a pretty icon) but I
    bet it's javaw.

    There was something tickling my brain as I was trying this out, but I
    didn't quite make the connection that javaw was being used as the
    handler, not java. Thanks for point that out.
     
    markspace, Apr 17, 2012
    #14
  15. Tim Slattery

    Tim Slattery Guest

    Tim Slattery <> wrote:

    >Tim Slattery <> wrote:
    >
    >>I must be doing something stupid.
    >>
    >>I have a very simple command-line program in Eclipse, just a
    >>System.out.println("Hello World"). It runs just fine in the IDE. So I
    >>use File|Export|Export, and select "Runnable JAR file". A JAR is
    >>created.
    >>
    >>And it doesn't work. I can double-click on it, or I can call it from a
    >>command line. I get nothing. No "Hello world", no error message, no
    >>nothing. What have I missed?

    >
    >More info:
    >Here's the class:
    >
    >
    >public class Main {
    > public static void main(String[] args)
    > {
    > System.out.println("Here I am!");
    > try
    > {
    > SimpleDateFormat sdf = new
    >SimpleDateFormat("MM/dd/yyyy");
    > sdf.setLenient(false);
    > Date mydate = sdf.parse("2/29/1900");
    > System.out.println("Good date: " + mydate.toString());
    > }
    > catch (ParseException ex)
    > {
    > System.out.println("ParseException: " +
    >ex.getMessage());
    > }
    >
    > }
    >
    > /* (non-Java-doc)
    > * @see java.lang.Object#Object()
    > */
    > public Main() {
    > super();
    > }
    >
    >I export "Main.jar". To invoke from the command line, I type
    >"Main.jar". Nothing but a command prompt. I have jedit installed. If I
    >go to its directory and type "jedit.jar", it jumps right up. Therefore
    >I assume that the JRE can be found.


    It looks like the problem is not that the program doesn't run, but
    that the output from "System.out.println" is being written on the
    wind. I included code to write a small file, and that file appears
    when I double-click the jar file. So the program runs, but where is
    the Standard Output file that System.out.println is supposed to write
    to?

    I could believe that it's on a console window that vanishes
    immediately when I start it be double-clicking it. But when I open a
    command window, navigate to the jar's directory and type "main.jar",
    the exact same thing happens. The file is created, but the System.out
    output is nowhere to be seen.

    --
    Tim Slattery
     
    Tim Slattery, Apr 17, 2012
    #15
  16. Tim Slattery

    markspace Guest

    On 4/17/2012 7:49 AM, Tim Slattery wrote:

    > It looks like the problem is not that the program doesn't run, but
    > that the output from "System.out.println" is being written on the
    > wind.



    Did you read the discussion above about "javaw"?
     
    markspace, Apr 17, 2012
    #16
  17. Tim Slattery

    Tim Slattery Guest

    markspace <-@.> wrote:

    >On 4/17/2012 7:49 AM, Tim Slattery wrote:
    >
    >> It looks like the problem is not that the program doesn't run, but
    >> that the output from "System.out.println" is being written on the
    >> wind.

    >
    >
    >Did you read the discussion above about "javaw"?


    You're right, I made my post before seeing the others. I wasn't aware
    of "java.exe" vs "javaw.exe". Thanks all for pointing it out.

    --
    Tim Slattery
     
    Tim Slattery, Apr 18, 2012
    #17
    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. Ralf W. Grosse-Kunstleve
    Replies:
    16
    Views:
    601
    Lonnie Princehouse
    Jul 11, 2005
  2. Ralf W. Grosse-Kunstleve
    Replies:
    18
    Views:
    606
    Bengt Richter
    Jul 11, 2005
  3. Ralf W. Grosse-Kunstleve
    Replies:
    2
    Views:
    415
    Dan Sommers
    Jul 12, 2005
  4. falcon
    Replies:
    0
    Views:
    386
    falcon
    Jul 31, 2005
  5. Bart Kastermans
    Replies:
    6
    Views:
    413
    Bart Kastermans
    Jul 13, 2008
Loading...

Share This Page