wood burning stoves 2.0*
The moose likes Java in General and the fly likes how to run java -jar with args? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to run java -jar with args?" Watch "how to run java -jar with args?" New topic
Author

how to run java -jar with args?

Nick Lee
Ranch Hand

Joined: Dec 17, 2002
Posts: 45
hi all,
now,I wanna run a program with this command:
java -jar runme.jar [mode]
I need use .property file to config this args.Can anybody tell how to deal with this?
thanks


Nick Lee
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
We use a .bat file and put the commands including the arguments in there. Then hook this to an icon for the user to click.
Of course that part would then be platform specific. You would need a .sh file for a unix machine.


"JavaRanch, where the deer and the Certified play" - David O'Meara
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60774
    
  65

Am I missing something? Why not just use:

?
bear


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Naren
Greenhorn

Joined: Jul 21, 2003
Posts: 23
Once you make a jar file and wish to use 'java -jar' command, you can not depend on system environment variable 'CLASSPATH' at runtime.
I assume you are having problems in accessing the properties file from the classes in the jar file. You can specify the the CLASSPATH valid only for that jar file using the manifest file included in the jar file.
Here are the steps to include CLASSPATH in your jar file:
1. Create a text file (lets call it mymanifest.txt) containing the following lines:
Class-Path: . a.jar b.jar
(Notice 2 spaces between each entry and an extra new line)
2. Create the jar file with:
jar cmvf mymanifest.txt my.jar a.class b.class
when you execute this jar file with java -jar command, it will have access to the CLASSPATH specified in the manifest.
The manifest can have a main class and a lot of other features....
Hope this helps.
Naren.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Originally posted by Bear Bibeault:
Am I missing something?
bear

I took it from the question that he wanted the user to be able to click something and invoke his application, only using input parameters.
You can double click a jar file and it will fire up, but you can not feed it the properties on fire up that he is requesting without either having the USER key in the properties file (ugly) or having some other mechanism just as a .bat file to provide that function.
Ellen Zhao
Ranch Hand

Joined: Sep 17, 2002
Posts: 581
Originally posted by Naren mqtips.com:

The manifest can have a main class and a lot of other features....

What's that? Thanks in advance.

Regards,
Ellen
Naren
Greenhorn

Joined: Jul 21, 2003
Posts: 23
When you make a jar file consisting of various java class files, you can identify one of them having the main() method using
Main-Class: classname
in the manifest. So when you execute 'java -jar yourapp.jar', the entry point is the main method in the class you mentioned in the manifest.
does that clear up?
I know who you are...
Nick Lee
Ranch Hand

Joined: Dec 17, 2002
Posts: 45
hi Naren,
When you make a jar file consisting of various java class files, you can identify one of them having the main() method using
Main-Class: classname
in the manifest. So when you execute 'java -jar yourapp.jar', the entry point is the main method in the class you mentioned in the manifest.

I know this,and problems now is I must run with this line:java -jar myjar.jar [mode].
[mode] which decide some runing sequence has been writen in the main method in the mainclass.
I wanna know if how to get the [mode] param from the command line to use in the main() of mainclass with a .property file.
All above is an order,I must follow it.
Am I desc it clearly?I'm a Chinese guy,sorry for my english :-)
Nick Lee
Ranch Hand

Joined: Dec 17, 2002
Posts: 45
hi Bear,
Am I missing something? Why not just use:
code:
--------------------------------------------------------------------------------
java -jar jarfile.jar properties-file.properties
--------------------------------------------------------------------------------
?

This is the order,I must run my app.jar in that mode.
Ellen Zhao
Ranch Hand

Joined: Sep 17, 2002
Posts: 581
Originally posted by Naren mqtips.com:
When you make a jar file consisting of various java class files, you can identify one of them having the main() method using
Main-Class: classname
in the manifest. So when you execute 'java -jar yourapp.jar', the entry point is the main method in the class you mentioned in the manifest.
does that clear up?

Yes, nothing can be clearer than that, thank you...but I think I didn't make myself understood, I meant what is the other features about manifest...say I have a project built automatically by Eclipse(RMI involed), but I have to specify the host and client's names and ports and many other parameters when run it, if I don't have the start.sh file. Can they also be done in the manifest? Thanks.
Originally posted by Naren mqtips.com:
I know who you are...

Naren, Naren, Naren, I was so glad to catch you here shortly after your very first post As soon as I saw your id I knew who was there talking Welcome and hope you enjoy the Ranch (You haven't forgotten java stuff completely? Good for you )
Sorry Nick, I've just got an old buddy here and the content above is off topic. Just ignore it, I won't do it any more.

Regards,
Ellen
[ July 22, 2003: Message edited by: Ellen Zhao ]
Naren
Greenhorn

Joined: Jul 21, 2003
Posts: 23
I don't understand your problem. I don't get the relationship between [mode] and the .properties file.
If you want to read/load the .properties file in your program, it must be present in one of the directories specified by the CLASSPATH.
 
 
subject: how to run java -jar with args?
 
Similar Threads
Error connecting to database from executable JAR file.
Specify classpath when run class in jar file
running a class in a jar ......
trouble creating jar file