943,914 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1938
  • Java RSS
Jan 6th, 2009
0

Interpreting JAR's launched with parameters

Expand Post »
Hey guys,
I am trying to get some code to interpret a parameter determined by a JAR file.

IE.
C:\user\myProgram.jar 500
I want to know how to transfer 500 over to the program. I googled it, and most of the stuff that came up was extremely difficult to understand.

Thanks PO
Reputation Points: 10
Solved Threads: 4
Junior Poster
PhiberOptik is offline Offline
164 posts
since May 2008
Jan 7th, 2009
0

Re: Interpreting JAR's launched with parameters

To tell you the truth, I don't know what you mean "interpret a parameter determined by a JAR file".

If it is simply to be an argument on the command line, then, of course, args[0].
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Jan 7th, 2009
0

Re: Interpreting JAR's launched with parameters

args[0] ?

Sorry i am confused, here I will give you a small rundown of the program. I use a program to automatically log myself into Steam. With steam you can launch their games by saying C:\Steam\steam.exe -login username password -applaunch 500 and of course the login and application vary between games. So I have a bunch of icons on my desktop that go to a JAR that logs me into steam automatically, but then I have to launch the game from inside of steam, so I want to know how to launch a JAR and have the jar grab the parameter. Basically I want to say C:\user\thatProgram.jar -500 and have the program look for the parameter and if it's there make it a int inside of my program.

Thanks Jon
Reputation Points: 10
Solved Threads: 4
Junior Poster
PhiberOptik is offline Offline
164 posts
since May 2008
Jan 7th, 2009
0

Re: Interpreting JAR's launched with parameters

Now if you have ever written a main, in your Java program, we write it as :-
java Syntax (Toggle Plain Text)
  1. public static void main(String args[]) {

Here the args, is an array containing the parameters you provided at the command line.

Consider the following example :-
java Syntax (Toggle Plain Text)
  1. public class CommandLineArgEx {
  2.  
  3. public static void main(String[] args) {
  4. for(int i=0;i<args.length;i++) {
  5. System.out.println("Argument Number " + (i+1) + " : " + args[i]);
  6. }
  7. }
  8.  
  9. }

Following would be the output that you will get :-

Java Syntax (Toggle Plain Text)
  1. stephen@steve:~/Development/java/daniweb> java CommandLineArgEx ABC Bcd Efg
  2. Argument Number 1 : ABC
  3. Argument Number 2 : Bcd
  4. Argument Number 3 : Efg
  5. stephen@steve:~/Development/java/daniweb>

So as you see whatever is passed via the command line, is stored in the args[] String array.
Although I have never tried it with a JAR, I do not see why it should not work.
Last edited by stephen84s; Jan 7th, 2009 at 2:12 pm.
Featured Poster
Reputation Points: 653
Solved Threads: 151
Nearly a Posting Virtuoso
stephen84s is offline Offline
1,316 posts
since Jul 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Determine instance
Next Thread in Java Forum Timeline: TurtleGraphics package





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC