• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to pass command line arguments  RSS feed

 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is how i start an application over command line....

command line commands:


now, i want to write a method (startTest()) in a class which i will place in the same package as TestApp.jar....

i want startTest() to pass the command line commands as parameters in string form...say, when a thread triggers it to go off.

how would i go about this?

could i maybe also do so by implementing a main method inside TestApp.jar... (i dont have the sources for TestApp.jar, so i'm a bit lost on that...)....

cheers

W.O.
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

could i maybe also do so by implementing a main method inside TestApp.jar... (i dont have the sources for TestApp.jar, so i'm a bit lost on that...)....


If you take a look at your command line, you will notice two things. First, you are actually not passing any parameters. Second, you actually don't know which class is being called.

Basically, there is no magic going on here. The JVM is simply calling calling the main() method as usual -- passing it the parameters, which in your case, there is none.

Now... to figure out the class, you need to extract the jar. Take a look at the manifest in the jar file for the "Main-Class". Once you figure out what the main class is... you can write your own class. Just make sure you choose a class that doesn't conflict with a class that is in the jar file.

Once you compile your new class, with the main() method that you want, you can add it to the jar file -- this is done by extracting the jar, adding your class, and recreating the jar file. Don't forget to change the Main-Class to your class in the manifest.

As for the original main, you can still call it. Recall that you have the name of the class (the original Main-Class), and you know the method is called main(). You can indirectly call that main method, from your main method.

Henry
[ January 23, 2008: Message edited by: Henry Wong ]
 
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Henry Wong:


If you take a look at your command line, you will notice two things. First, you are actually not passing any parameters. Second, you actually don't know which class is being called.

Basically, there is no magic going on here. The JVM is simply calling calling the main() method as usual -- passing it the parameters, which in your case, there is none.

Now... to figure out the class, you need to extract the jar. Take a look at the manifest in the jar file for the "Main-Class". Once you figure out what the main class is... you can write your own class. Just make sure you choose a class that doesn't conflict with a class that is in the jar file.

Once you compile your new class, with the main() method that you want, you can add it to the jar file -- this is done by extracting the jar, adding your class, and recreating the jar file. Don't forget to change the Main-Class to your class in the manifest.

As for the original main, you can still call it. Recall that you have the name of the class (the original Main-Class), and you know the method is called main(). You can indirectly call that main method, from your main method.

Henry

[ January 23, 2008: Message edited by: Henry Wong ]


I am just presenting my understanding of Henry's post:

Whatever arguments you pass with following command:



Goes into "array" of main method of "Main-Class" of your JAR file. And you can retrieve them like this:



And then do whatever you want, call your startTest() method or whatever...

Now how to make a class "Main-Class" in JAR file, I don't know.

 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by ankur rathi:
Now how to make a class "Main-Class" in JAR file, I don't know.


Henry explained that in his post.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!