Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Command-line options in a jar file

Reply
Thread Tools

Command-line options in a jar file

 
 
Hiram Hunt
Guest
Posts: n/a
 
      04-21-2012
Hello,

Is there a way to pass command-line options to java.exe
with an executable jar file? The "Jar tool reference
page for Windows" seems to say that the -J option should
do it, but when I try it out, the option is sent to the
java virtual machine when I run the jar command, not when
I run the executable jar file. I want to send -Xmxn and
-Dthis=that options to java.exe.

Trying things out with a simple HelloWorld program, when I
use -J-showversion or -J-verbose, the extra output comes
when the jar file is being created, not when it is being
run. I was using Windows SP3 and Java 7u3. I also tried
under Windows 7 a program that prints system properties
(retrieved with System.getProperties()) under Windows 7
and a -J-Dthis=that option, but the property did not appear
in the output.

Specifically, with Windows SP3 and the command prompt:

#javac hi\HelloWorld.java

#jar cfe hi.jar hi.HelloWorld hi\HelloWorld.class -J-showversion
java version "1.7.0_03"
Java(TM) SE Runtime Environment (build 1.7.0_03-b05)
Java HotSpot(TM) Client VM (build 22.1-b02, mixed mode, sharing)


#ftype jarfile
jarfile="C:\Program Files\Java\jre7\bin\java.exe" -jar "%1%" %*

#hi.jar
Hello, world

#

Is there a way to (in this example) get the version information
to show up when running hi.jar instead of when running jar itself?

-- Hiram Hunt


 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      04-21-2012
On 04/21/2012 12:11 PM, Hiram Hunt wrote:
> Hello,
>
> Is there a way to pass command-line options to java.exe
> with an executable jar file? The "Jar tool reference
> page for Windows" seems to say that the -J option should
> do it, but when I try it out, the option is sent to the
> java virtual machine when I run the jar command, not when
> I run the executable jar file. I want to send -Xmxn and
> -Dthis=that options to java.exe.


Pass them on the command line with the "java" command.

> Trying things out with a simple HelloWorld program, when I
> use -J-showversion or -J-verbose, the extra output comes
> when the jar file is being created, not when it is being
> run. I was using Windows SP3 and Java 7u3. I also tried
> under Windows 7 a program that prints system properties
> (retrieved with System.getProperties()) under Windows 7
> and a -J-Dthis=that option, but the property did not appear
> in the output.
>
> Specifically, with Windows SP3 and the command prompt:
>
> #javac hi\HelloWorld.java


Please don't use backslashes as path separators, at least not here. It's very
disconcerting as they have special meaning for Java and non-Windows shells.
Windows handles forward slashes as path separators, so there's no reason not
to use them in Usenet posts.

> #jar cfe hi.jar hi.HelloWorld hi\HelloWorld.class -J-showversion
> java version "1.7.0_03"
> Java(TM) SE Runtime Environment (build 1.7.0_03-b05)
> Java HotSpot(TM) Client VM (build 22.1-b02, mixed mode, sharing)
>
>
> #ftype jarfile
> jarfile="C:\Program Files\Java\jre7\bin\java.exe" -jar "%1%" %*
>
> #hi.jar
> Hello, world
>
> #
>
> Is there a way to (in this example) get the version information
> to show up when running hi.jar instead of when running jar itself?


Version of what? Java itself, as you show here? What's wrong with using the
"java" command for that?

<http://docs.oracle.com/javase/7/docs/>

Does Windows 7 let you specify command options with the file association? I
never invoke JARs without an explicit invocation of the "java" command; I
suggest you follow that practice.

You can put the invocation in a shell script and pass all the options your
little heart desires to the "java" command. That seems like the obvious
approach, yes?

--
Lew
Honi soit qui mal y pense.
http://upload.wikimedia.org/wikipedi.../c/cf/Friz.jpg
 
Reply With Quote
 
 
 
 
Martin Gregorie
Guest
Posts: n/a
 
      04-21-2012
On Sat, 21 Apr 2012 15:11:14 -0400, Hiram Hunt wrote:

> Is there a way to pass command-line options to java.exe with an
> executable jar file? The "Jar tool reference page for Windows" seems to
> say that the -J option should do it, but when I try it out, the option
> is sent to the java virtual machine when I run the jar command, not when
> I run the executable jar file. I want to send -Xmxn and -Dthis=that
> options to java.exe.
>

Use the "Main-Class: full.class.name" directive in the jarfile's manifest
to select the program to be executed and then run it with the command:

java -jar jarfile_absolute_name arguments...

which passes the argument list to "static void main(String[] args) in
full.class.name via args. If you need to pass options to the JVM, they
are put in front of the -jar option.

If I want to be able to choose one candidate program out of several in
the jar file, I use the Main-Class to run a simple launcher program and
pass it the program name as its first argument. The launcher program uses
Class.forName() to load the required program and then starts it by
calling a method, which must not have the "static void main(String[])"
signature. I typically use via a method with the "void run(String[] args)"
signature and pass it all the command line arguments except the first via
args.


--
martin@ | Martin Gregorie
gregorie. | Essex, UK
org |
 
Reply With Quote
 
Hiram Hunt
Guest
Posts: n/a
 
      04-21-2012

"Lew" <(E-Mail Removed)> wrote in message
news:jmv4la$b2f$(E-Mail Removed)...
> On 04/21/2012 12:11 PM, Hiram Hunt wrote:
>> Hello,
>>
>> Is there a way to pass command-line options to java.exe
>> with an executable jar file? The "Jar tool reference
>> page for Windows" seems to say that the -J option should
>> do it, but when I try it out, the option is sent to the
>> java virtual machine when I run the jar command, not when
>> I run the executable jar file. I want to send -Xmxn and
>> -Dthis=that options to java.exe.

>
> Pass them on the command line with the "java" command.


I have done that, but that is not what I am trying to do now.

>
>> Trying things out with a simple HelloWorld program, when I
>> use -J-showversion or -J-verbose, the extra output comes
>> when the jar file is being created, not when it is being
>> run. I was using Windows SP3 and Java 7u3. I also tried
>> under Windows 7 a program that prints system properties
>> (retrieved with System.getProperties()) under Windows 7
>> and a -J-Dthis=that option, but the property did not appear
>> in the output.
>>
>> Specifically, with Windows SP3 and the command prompt:
>>
>> #javac hi\HelloWorld.java

>
> Please don't use backslashes as path separators, at least not here. It's
> very disconcerting as they have special meaning for Java and non-Windows
> shells. Windows handles forward slashes as path separators, so there's no
> reason not to use them in Usenet posts.


This was actual cut and paste of a Windows session. In this case, forward
slashes would work with javac, but it is unnatural when using the Windows
command prompt because they are not generally accepted there. For
example, they will not work in the ftype file association shown below.

>
>> #jar cfe hi.jar hi.HelloWorld hi\HelloWorld.class -J-showversion
>> java version "1.7.0_03"
>> Java(TM) SE Runtime Environment (build 1.7.0_03-b05)
>> Java HotSpot(TM) Client VM (build 22.1-b02, mixed mode, sharing)
>>
>>
>> #ftype jarfile
>> jarfile="C:\Program Files\Java\jre7\bin\java.exe" -jar "%1%" %*
>>
>> #hi.jar
>> Hello, world
>>
>> #
>>
>> Is there a way to (in this example) get the version information
>> to show up when running hi.jar instead of when running jar itself?

>
> Version of what? Java itself, as you show here? What's wrong with using
> the "java" command for that?


I used -version to demonstrate the problem I was having getting a
command-line option to be saved in a jar file for use when the
jar file is executed. The specific option I used for the demonstration
was just a convenient example, because the results can easily be
seen. As I said before, the options I actually care about (today, anyway)
are -Xmxn and -Dthis=that (with appropriate values to be substituted
for "n" and "this=that").

>
> <http://docs.oracle.com/javase/7/docs/>
>
> Does Windows 7 let you specify command options with the file association?
> I never invoke JARs without an explicit invocation of the "java" command;
> I suggest you follow that practice.


I could (I think) add the command line options to the file association (I
have
already changed it from javaw.exe to java.exe), but then the options would
apply to each jar file I execute.

>
> You can put the invocation in a shell script and pass all the options your
> little heart desires to the "java" command. That seems like the obvious
> approach, yes?
>


Very obvious, so yes I had already thought of it and used it, though mainly
with .class files that were not in a jar file, but I am asking whether
there is
a way to pass the options in a jar file. Alternatively, I think I can set
the
property in the executable with System.setProperty(), but I am not sure
what command could be used to set the memory option (-Xmxn) from
within the program.

-- Hiram Hunt


 
Reply With Quote
 
Hiram Hunt
Guest
Posts: n/a
 
      04-21-2012

"Martin Gregorie" <(E-Mail Removed)> wrote in message
news:jmv8c3$fka$(E-Mail Removed)...
> On Sat, 21 Apr 2012 15:11:14 -0400, Hiram Hunt wrote:
>
>> Is there a way to pass command-line options to java.exe with an
>> executable jar file? The "Jar tool reference page for Windows" seems to
>> say that the -J option should do it, but when I try it out, the option
>> is sent to the java virtual machine when I run the jar command, not when
>> I run the executable jar file. I want to send -Xmxn and -Dthis=that
>> options to java.exe.
>>

> Use the "Main-Class: full.class.name" directive in the jarfile's manifest
> to select the program to be executed and then run it with the command:
>
> java -jar jarfile_absolute_name arguments...
>
> which passes the argument list to "static void main(String[] args) in
> full.class.name via args. If you need to pass options to the JVM, they
> are put in front of the -jar option.
> ....


Thanks. Yes, I know I can put options before the -jar option, but I would
like to be able to just run the program as "MyProgram.jar" by using the
ftype association in my original post. Thus, my question is about passing
arguments for java.exe in the jar file. My guess at this point is that it
is
not possible.

-- Hiram Hunt


 
Reply With Quote
 
Jan Burse
Guest
Posts: n/a
 
      04-21-2012
Hiram Hunt schrieb:
> As I said before, the options I actually care about (today, anyway)
> are -Xmxn and -Dthis=that (with appropriate values to be substituted
> for "n" and "this=that").


Hi,

The syntax the java command line in case of a .jar is:

java [ options ] -jar file.jar [ argument ... ]

The -X and the -D option can be placed before the -jar.
But I also did not find a method to place it into a
manifest. Definition for:

-Dproperty=value

Is that it will set a system property value. So right,
this can be done inside your .jar. And arguments are
passed to main() this can be also done inside your .jar.
But for the -X options I don't see a way, since:

Options that begin with -X are non-standard
(not guaranteed to be supported on all VM
implementations), and are subject to change
without notice in subsequent releases of
the JDK.

To modify a -X option inside your .jar seems hope
less. Not because they are non standard. But because
most of them are immutable and take effect when the
JVM is initialized. So when your main() is invoked
it is already too late.

I was once hoping that I can for example change the
heap size via a management bean. But I didn't find
an appropriate method here:

Package java.lang.management
http://docs.oracle.com/javase/7/docs...e-summary.html

So I guess what some tools do is that they fork
a Process and pass the parameters they want. This
can be done inside a jar.

Class ProcessBuilder

http://docs.oracle.com/javase/1.5.0/...ssBuilder.html

But this seems also risky and ugly, same as writing
small scripts. The following stack overflow answer
additionally lists launch4j and JavaWeb Start as
an alternative:

http://stackoverflow.com/questions/1...rom-a-jar-file

Didn't try either...

Bye


 
Reply With Quote
 
Knute Johnson
Guest
Posts: n/a
 
      04-21-2012
On 4/21/2012 12:11 PM, Hiram Hunt wrote:
> Hello,
>
> Is there a way to pass command-line options to java.exe
> with an executable jar file? The "Jar tool reference
> page for Windows" seems to say that the -J option should
> do it, but when I try it out, the option is sent to the
> java virtual machine when I run the jar command, not when
> I run the executable jar file. I want to send -Xmxn and
> -Dthis=that options to java.exe.
>
> Trying things out with a simple HelloWorld program, when I
> use -J-showversion or -J-verbose, the extra output comes
> when the jar file is being created, not when it is being
> run. I was using Windows SP3 and Java 7u3. I also tried
> under Windows 7 a program that prints system properties
> (retrieved with System.getProperties()) under Windows 7
> and a -J-Dthis=that option, but the property did not appear
> in the output.
>
> Specifically, with Windows SP3 and the command prompt:
>
> #javac hi\HelloWorld.java
>
> #jar cfe hi.jar hi.HelloWorld hi\HelloWorld.class -J-showversion
> java version "1.7.0_03"
> Java(TM) SE Runtime Environment (build 1.7.0_03-b05)
> Java HotSpot(TM) Client VM (build 22.1-b02, mixed mode, sharing)
>
>
> #ftype jarfile
> jarfile="C:\Program Files\Java\jre7\bin\java.exe" -jar "%1%" %*
>
> #hi.jar
> Hello, world
>
> #
>
> Is there a way to (in this example) get the version information
> to show up when running hi.jar instead of when running jar itself?
>
> -- Hiram Hunt


I don't think the -J option does what you think it does. It does not
appear to cause the launcher, java.exe, to use the option you specified
with the -J when you compiled it or jar'd it. I think it passes that to
the runtime that does the compiling and jar'ing. To be specific in your
example above, I think the -showversion option was passed to the java
runtime that was used to by the jar.exe program to create your jar file.
And that is why it shows up when you run it as above.

If you want a command line option when you run your program, you will
have to pass the arguments on the command line or by Patricia's method
in a call to the main() method.

--

Knute Johnson
 
Reply With Quote
 
Hiram Hunt
Guest
Posts: n/a
 
      04-21-2012

"Patricia Shanahan" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) m...
> On 4/21/2012 2:43 PM, Hiram Hunt wrote:
> ...
>> Thanks. Yes, I know I can put options before the -jar option, but I
>> would
>> like to be able to just run the program as "MyProgram.jar" by using the
>> ftype association in my original post. Thus, my question is about
>> passing
>> arguments for java.exe in the jar file. My guess at this point is that
>> it
>> is
>> not possible.

>
> Remember that a Java main is just a static method, and can be called. You
> could write, include in your jar, and make the main class something like
> this:
>
> =====WARNING===UNTESTED=CODE========
> public class Wrapper {
> private static String[] actualArgs = {
> "arg1",
> "arg2",
> }
> public static void main(String[] args) {
> HelloWorld.main(actualArgs);
> }
> }
> ====================================
>


Thanks, but those are not the arguments I am trying to
work with. I am trying to pass arguments to the java
virtual machine, not to main(). I want to use -Xmxn and
-Dthis=that (with appropriate substitutions for "n" and
"this=that"). Java.exe needs to see them. Alternatively,
I think I could use "System.setProperty()" first in main(),
but I don't know what I could call in main() to produce
the same effect as -Xmxn. The -Dthis=that is to prevent
a conflict that might only occur _rarely_ in a library I
am using, so I would have a hard time checking directly
whether setting it in main() is really soon enough.
The actual option is -Dsun.java2d.noddraw=true .

I am hoping to be able to run the program in a "pretty"
way -- just type MyProgram.jar without having to give
certain fixed, mandatory arguments every time and without
resorting to a batch file. In the end, I don't think
it will be possible. It is not a show stopper, just an
annoyance.

-- Hiram Hunt


 
Reply With Quote
 
Hiram Hunt
Guest
Posts: n/a
 
      04-21-2012

"Knute Johnson" <(E-Mail Removed)> wrote in message
news:jmvckp$866$(E-Mail Removed)...
> On 4/21/2012 12:11 PM, Hiram Hunt wrote:
>> Hello,
>>
>> Is there a way to pass command-line options to java.exe
>> with an executable jar file? The "Jar tool reference
>> page for Windows" seems to say that the -J option should
>> do it, but when I try it out, the option is sent to the
>> java virtual machine when I run the jar command, not when
>> I run the executable jar file. I want to send -Xmxn and
>> -Dthis=that options to java.exe.
>>
>> Trying things out with a simple HelloWorld program, when I
>> use -J-showversion or -J-verbose, the extra output comes
>> when the jar file is being created, not when it is being
>> run. I was using Windows SP3 and Java 7u3. I also tried
>> under Windows 7 a program that prints system properties
>> (retrieved with System.getProperties()) under Windows 7
>> and a -J-Dthis=that option, but the property did not appear
>> in the output.
>>
>> Specifically, with Windows SP3 and the command prompt:
>>
>> #javac hi\HelloWorld.java
>>
>> #jar cfe hi.jar hi.HelloWorld hi\HelloWorld.class -J-showversion
>> java version "1.7.0_03"
>> Java(TM) SE Runtime Environment (build 1.7.0_03-b05)
>> Java HotSpot(TM) Client VM (build 22.1-b02, mixed mode, sharing)
>>
>>
>> #ftype jarfile
>> jarfile="C:\Program Files\Java\jre7\bin\java.exe" -jar "%1%" %*
>>
>> #hi.jar
>> Hello, world
>>
>> #
>>
>> Is there a way to (in this example) get the version information
>> to show up when running hi.jar instead of when running jar itself?
>>
>> -- Hiram Hunt

>
> I don't think the -J option does what you think it does. It does not
> appear to cause the launcher, java.exe, to use the option you specified
> with the -J when you compiled it or jar'd it. I think it passes that to
> the runtime that does the compiling and jar'ing. To be specific in your
> example above, I think the -showversion option was passed to the java
> runtime that was used to by the jar.exe program to create your jar file.
> And that is why it shows up when you run it as above.
>
> If you want a command line option when you run your program, you will have
> to pass the arguments on the command line or by Patricia's method in a
> call to the main() method.
>


Thanks. Actually, I already had figured out what -J was doing.
I showed in my original post what it was doing in hopes that people
who had not tried it would not reply that -J was the answer to my
problem.

-- Hiram Hunt


 
Reply With Quote
 
Hiram Hunt
Guest
Posts: n/a
 
      04-21-2012

"Jan Burse" <(E-Mail Removed)> wrote in message
news:jmvcig$q1c$(E-Mail Removed)...
> Hiram Hunt schrieb:
>> As I said before, the options I actually care about (today, anyway)
>> are -Xmxn and -Dthis=that (with appropriate values to be substituted
>> for "n" and "this=that").

>
> Hi,
>
> The syntax the java command line in case of a .jar is:
>
> java [ options ] -jar file.jar [ argument ... ]
>
> The -X and the -D option can be placed before the -jar.
> But I also did not find a method to place it into a
> manifest. Definition for:
>
> -Dproperty=value
>
> Is that it will set a system property value. So right,
> this can be done inside your .jar. And arguments are
> passed to main() this can be also done inside your .jar.
> But for the -X options I don't see a way, since:
>
> Options that begin with -X are non-standard
> (not guaranteed to be supported on all VM
> implementations), and are subject to change
> without notice in subsequent releases of
> the JDK.
>
> To modify a -X option inside your .jar seems hope
> less. Not because they are non standard. But because
> most of them are immutable and take effect when the
> JVM is initialized. So when your main() is invoked
> it is already too late.
>
> I was once hoping that I can for example change the
> heap size via a management bean. But I didn't find
> an appropriate method here:
>
> Package java.lang.management
> http://docs.oracle.com/javase/7/docs...e-summary.html
>
> So I guess what some tools do is that they fork
> a Process and pass the parameters they want. This
> can be done inside a jar.
>
> Class ProcessBuilder
>
> http://docs.oracle.com/javase/1.5.0/...ssBuilder.html
>
> But this seems also risky and ugly, same as writing
> small scripts. The following stack overflow answer
> additionally lists launch4j and JavaWeb Start as
> an alternative:
>
> http://stackoverflow.com/questions/1...rom-a-jar-file
>
> Didn't try either...
>
> Bye
>
>


Thanks. The stackoverflow link gets at the sort of thing I am
trying to do. I don't see in the ProcessBuilder documentation
a way to set memory size, though it does let one set system
properties. I think I should just settle for running things in
a less pretty way (batch file, e.g.) than I would like. Maybe
someday a new option will be added for jar files to do this.

-- Hiram Hunt


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
java -cp a.jar -jar b.jar => Works on Windows, not on Debian cyberco Java 4 02-14-2006 06:27 AM
jaas.jar, jta.jar jdbc-stdext.jar missing from jdk1.5 RPM muttley Java 0 10-20-2005 02:40 PM
Differences of xercesImpl.jar, xercesImpl-J.jar, dom3-xercesImpl.jar ? Arnold Peters Java 0 01-05-2005 10:59 PM
Differences of xercesImpl.jar, xercesImpl-J.jar, dom3-xercesImpl.jar ? Arnold Peters XML 0 01-05-2005 10:59 PM
how to disassembly a .jar file? how to see what are the classes inside the .jar file? lucy Java 6 09-07-2004 09:54 PM



Advertisments