Solved

Work with parameter

Posted on 2015-02-10
17
121 Views
Last Modified: 2015-02-11
Hi,
Is there any example to pass 2 parameter to the executable jar file? what to adjust below?

public static void main(String[] args)
...

Open in new window

0
Comment
Question by:HuaMinChen
  • 8
  • 6
  • 3
17 Comments
 
LVL 10

Expert Comment

by:Jeffrey Dake
ID: 40602442
Can you give a little more detail on what you are trying to do?
0
 
LVL 10

Author Comment

by:HuaMinChen
ID: 40602449
I want to provide 2 values to the project, when running it. Since I still do not have the way to prompt for 2 input values (by previous thread), this is why now I expect to directly pass 2 values as parameter to the project. Thanks.
0
 
LVL 26

Accepted Solution

by:
dpearson earned 334 total points
ID: 40602451
You can pass in the parameters like this when you run the jar:

java -jar myjar.jar param1 param2

And then in your main method they will be in the args array:

public static void main(String[] args) {
    System.out.println(args.length) ;
}

Hope that helps,

Doug
0
 
LVL 10

Author Comment

by:HuaMinChen
ID: 40602454
Thanks. when running it, do I need to quote each string parameter with "?
0
 
LVL 10

Author Comment

by:HuaMinChen
ID: 40602457
How to correct the attached, due to this line?

		if (args[1].value=="")
...

Open in new window

t979.png
0
 
LVL 10

Expert Comment

by:Jeffrey Dake
ID: 40602460
Yes, you can pass in as many parameters you want that way. They will be in the array which is 0 based so you can get the values as

String paramOne = args[0];
String paramTwo = args[1];

Open in new window

0
 
LVL 26

Assisted Solution

by:dpearson
dpearson earned 334 total points
ID: 40602461
args[] is an array of Strings and in Java Strings don't have a "value" method.

So you want something like

if (args[0].equals("")) {
     ...
}

or

if (args[0].isEmpty()) {
    ...
}

And yes you should put quotes around the parameters when passing them in.

Doug
0
 
LVL 10

Author Comment

by:HuaMinChen
ID: 40602463
Thanks. Is there any way to prompt for any input value directly, within the Eclipse Java project?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 10

Assisted Solution

by:Jeffrey Dake
Jeffrey Dake earned 166 total points
ID: 40602465
Yes, you can setup run configurations. Within that you can set the arguments.

http://www.tutorialspoint.com/eclipse/eclipse_run_configuration.htm
0
 
LVL 26

Expert Comment

by:dpearson
ID: 40602476
Jeffrey is right that this is how to setup a run configuration.

I think you're specifically asking how to have that configuration prompt you for parameters.  To get that to happen you need to enter specific values as the parameters like "${string_prompt}" so that then Eclipse will prompt you for the value each time you run.

This is explained in more detail here:
http://www.oxfordmathcenter.com/drupal7/node/601

Doug
0
 
LVL 10

Author Comment

by:HuaMinChen
ID: 40602477
I get this
Z:\cmp2\Eclipse\JLib4>java -jar ItAcctNoValidator_049.jar "048" "0750-105633-462"
no main manifest attribute, in ItAcctNoValidator_049.jar

Open in new window

and here are the codes
package com.hkicl.ecp.hkanvm.b049;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class ItAcctNoValidator_049 {
	public static void main(String[] args) {
		String par1=args[0];
		String par2=args[1];
		int ret_val;
		String ret;
		
		...
		ret_val=ValAcctNo(par1,par2);
		ret="Return value: "+String.valueOf(ret_val);
		JOptionPane.showMessageDialog(null,
			    ret);
		System.exit(0);
	}
	public static int ValAcctNo (String ItCode,
			String ItAcctNo)
	{
		...

Open in new window

0
 
LVL 10

Author Comment

by:HuaMinChen
ID: 40602478
Many thanks.
Please disregard my last reply.
0
 
LVL 26

Expert Comment

by:dpearson
ID: 40602480
OK if you work through the example I posted above (from oxfordmathcenter) I think it's showing you everything you need.

Good luck,

Doug
0
 
LVL 10

Author Comment

by:HuaMinChen
ID: 40602481
To the attached, can we have more than one program argument? How about that we have more than one parameter as the input?
t980.png
0
 
LVL 26

Expert Comment

by:dpearson
ID: 40602490
I've not personally tried it (I don't use Eclipse any more) - but you should be able to use "${string_prompt} ${string_prompt}" to get it to ask for 2 params.

Did you try that?
0
 
LVL 10

Author Comment

by:HuaMinChen
ID: 40602504
I have 2 arguments to it like the attached and then have re-generated the jar file. But I still get this

Z:\cmp2\Eclipse\JLib4>java -jar ItAcctNoValidator_049.jar
no main manifest attribute, in ItAcctNoValidator_049.jar

Open in new window

t981.png
0
 
LVL 26

Expert Comment

by:dpearson
ID: 40602576
The error you're getting isn't anything to do with passing parameters to your code - it's telling you that the Jar file you're running wasn't built properly.

I think you're getting the 2 sets of help muddled together.

If you're running a Jar file that you create yourself, then you use the "Java -jar myjar.jar param1 param2" form.  You do this when you're not using Eclipse.

If you're running inside Eclipse you don't need to generate a Jar file at all.  Instead you create a run configuration and use that to pass the parameters to your Eclipse project.

You don't want to try to do both at the same time.

Doug
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
reasons why a "inside Menu" would not appear for a web server 6 55
Win 7 OS unable to install Win updates 3 135
stackato and cloud 4 64
fibonacci ten numbers 4 22
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
When you upgrade from Windows 8 to 8.1 or to Windows 10 or if you are like me you are on the Insider Program you may find yourself with many 450MB recovery partitions.  With a traditional disk that may not be a problem but with relatively smaller SS…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now