In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you how Java input parameters to the main function main, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
The meaning of passing the parameter String [] args into the main function main
String [] args is an array type parameter. Passing a parameter to the main function is equivalent to passing an array element to the array args.
The first step
Select the class name and click run as > run configurations. The following screen appears:
Step two
Select the class name and click Arguments to display the following screen:
Then enter data 525, which is equivalent to an args array of type String, passing in 5 for the first element, 2 for the second element, and 5 for the third element
Step three
Click Run > and the following results appear
Description: args [0] = 5 Personnal ARGs [1] = 2 cogent ARGs [2] = 5
Public static void main (String [] args) {int a, b, c; a = Integer.parseInt (args [0]); / / need to pass the parameter b = Integer.parseInt (args [1]); / / run configurations; c = Integer.parseInt (args [2]) to the main function; int I = sum (a, b, c) System.out.println (a + "+" + b + "*" + c + "=" + I);} public static int sum (int a, int b, int c) {int sum = 0; sum = a + mul (b, c); return sum;} public static int mul (int b, int c) {int mul = b * c; return mul;} Java main method
Java's main method is most familiar to Java programmers, because most of the time, we need to use this method to start the program, and the parameter list of the main method is String [] args, an array of strings, so how do we pass in the parameters we need when starting? here are three methods.
Program sample code:
Public class GreenLeaf {public static final String TYPE = "WhiteFlower"; public static void main (String [] args) {for (String a: args) {System.out.println (a);} System.out.println (TYPE);}}
The simple sample code above prints the passed-in parameters and a string.
Use the IDE integrated development environment to run the Java main method (Idea presentation)
1. First, click the menu Run- > Edit Configurations:
two。 Then, enter your parameters in the Configuration-> Program arguments of the main class, with multiple parameters separated by spaces:
Just click Save.
Second, use the command java-jar * * .jar to run the Java program
How to pass the parameter solution:
0. First of all, you need to type a jar bag correctly:
Many people may have this problem when packing: "there is no main list attribute in White-1.0-SNAPSHOT.jar". The author uses maven to manage the project, and the solution is to add the following plug-ins to the pom file:
Org.apache.maven.plugins maven-jar-plugin true cn.itoak.white.GreenLeaf
1. Then run it under the project root directory: the mvn clean package command will type the jar package correctly, and the package is located in the following location:
two。 Go to the target directory and run the command:
Java-jar White-1.0-SNAPSHOT.jar 1 2 3 3. Use the mvn command to run the Java program and pass parameters
0. Before executing the command, make sure that you have compiled (mvn compile) correctly:
1. Then execute the command under the project root:
Mvn exec:java-Dexec.mainClass= "cn.itoak.white.GreenLeaf"-Dexec.args= "1 2 3"
One hour. If you do not want to specify-Dexec.mainClass in the command, you need to add the following plug-in to the pom file and specify mainClass:
Org.codehaus.mojo exec-maven-plugin 1.6.0 cn.itoak.white.GreenLeaf false
If you add the above plug-in, you only need to execute the following command:
Mvn exec:java-Dexec.args= "1 2 3" 4. Execute .class files directly (learn about it, rarely used)
0. Compile and generate the .class file first:
Javac GreenLeaf.java
1. Execute:
Java GreenLeaf 1 2 3
Final implementation result:
These are all the contents of the article "how to pass parameters to the main function main by Java". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.