Self-executing JAR

T

Tim Slattery

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?
 
D

Daniel Pitts

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.
 
M

markspace

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....
 
T

Tim Slattery

Tim Slattery said:
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.
 
L

Lew

Tim said:
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?
 
K

Knute Johnson

Tim Slattery said:
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
 
M

markspace

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!" );
}
}
 
M

markspace

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.
 
L

Lew

Tim said:
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?
 
A

Andreas Leitgeb

markspace said:
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)
 
R

Roedy Green

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..
 
T

Tim Slattery

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.
 
M

markspace

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.
 
T

Tim Slattery

Tim Slattery said:
Tim Slattery said:
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.
 
M

markspace

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"?
 
T

Tim Slattery

markspace said:
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.
 

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,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top