This question already has answers here:
How do I parse command line arguments in Java?
(21 answers)
Closed 6 years ago.
I'm coding a tool in java that need some input passed by args[]
I know how to use argument but i wish to handle better this input.
this is a part of my code:
if (args.length > 0 ) {
switch (args[0]) {
case "scan": blah blah
break;
case "some cases": some code
break;
but of course args are strictly bounded to their position, in fact in the command line i have to call:
java javaProgram arg1 arg2 arg3
i really don't like this...
What i want is to better manage this arguments with options like any other c programmed tool, indipendent from positions:
example:
java javaProgram -ip 127.0.0.1 -database data.txt -vv -out output.txt
any help will be appreciated!
Dave
(i'm thinking about an array list of arguments, can be a solution?)
Use Apache Commons CLI Builder .
Related
So I need to make a java program that represents a bank tiller. However, I need to use an executable script that will feed the command line arguments to the java program. Unfortunately, there are multiple types of commands I can do that would need to call the java program.
Since there are different types of command options (start, buy, and change, I do not know how I could go about feeding the right argument information to the java program. Any assistance would be greatly appreciated.
Unless I'm missing something, you could use $# to pass the script's arguments to your Java program. For example,
#!/usr/bin/env bash
export CLASSPATH="$HOME/src/java/"
java com.example.MyTeller "$#"
Pass the script arguments to your Java program:
java programName "$#"
Here is a sample:
public class PrintArgs {
public static void main (String[] args) {
for (int x=0; x<args.length; x++) {
System.out.println(arg[x]);
}
}
}
Call it like this:
java PrintArgs start 80 = 10 2 2 2
The script I am not that sure about, but I you can look it up. Google shell scripts arguments.
Take a look at the Apache CLI stuff. Specifically the POSIX parser (http://www.javaworld.com/article/2072482/command-line-parsing-with-apache-commons-cli.html)
It will enable you to specify POSIX style command line arguments (--buy {value} --sell {value})...
This question already has answers here:
How to make pipes work with Runtime.exec()?
(4 answers)
Closed 8 years ago.
I am new to java and learning it. I want to run a command line program from java class.
This is the command i want to run:(only linux)
path/to/folder/$ echo "Inhibition of NF-kappaB activation reversed the anti-apoptotic effect of isochamaejasmin." | ./geniatagger
This will give me output which I want to store in a java object.
String output:
Inhibition Inhibition NN B-NP O
of of IN B-PP O
NF-kappaB NF-kappaB NN B-NP B-protein
activation activation NN I-NP O
reversed reverse VBD B-VP O
the the DT B-NP O
anti-apoptotic anti-apoptotic JJ I-NP O
effect effect NN I-NP O
of of IN B-PP O
isochamaejasmin isochamaejasmin NN B-NP O
. . . O O
Please guide me how to achieve that?
Command line interpreters, mostly called shells, are a typical platform specific feature not supported well by Java. This is not to say it is impossible, but you have to ask yourself whether you want this feature to work on all platforms or not.
If you don't care about multi-platform, you can look into Runtime.exec
E.g:
Runtime.getRuntime().exec("cmd /c echo Hello World!");
If you do care, maybe you are better off doing the same thing from Java directly. Mostly shell commands can be achieved from Java directly without too much code. It will then be cross-platform and the performance will often be much better.
If you decide to go with invocation of shell commands anyway, to act on the output of it, learn how to access the standard input/output streams. Look at this example.
UPDATE
I think I now see that probably you want to write a program ./geniatagger that will accept input from the command line, e.g. output of 'echo' that was piped to it etc?
If so, look at using System.in, the standard input stream.
This question already has answers here:
What is the "String args[]" parameter in the main method?
(18 answers)
Closed 9 years ago.
I am a new programer and I'm just starting to learn the basics of Java and I'm trying to understand what exactly "args" stands for in "public static void main(String[] args)".
I found that's it's connected to command line arguments, which I don't understand. I would like to know what "args" means.
Thank you.
When you run a Java program, it usually looks like this:
java MyProgram
However, you also have the option of including command-line arguments. For example, if your program adds two numbers, you could set it up to take input like this:
java MyProgram 12 47
In this case, arr would equal ["12", "47"]. Having input work in this way is useful because it makes it easier to automate the running of your program through batch files or the like.
args is an arbitrary name for command line arguments. Running a java class file from the command line will pass an array of Strings to the main() function. If you want to handle extra arguments, you can check for keywords at certain indices of args and perform extra functions based on them.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is “String args[]”? in Java
So I recently (well, three days ago) started teaching myself Java, with absolutely zero previous programming experience unless you count HTML/CSS.
Now I've been looking through both books and online tutorials, including the Sun tutorials. I don't quite understand what the purpose of (string args[]) in a method is.
What I've got from looking online is that when using the console to run a program, it substitutes whatever comes after ">Java myprogram" in place of the "args".
Firstly, is that correct? If so, why would you do that?
Cheers!
The String[] args which can be written as String args[] or String ... args is an array of the arguments you gave the program.
why would you do that?
So you can give your program inputs on the command line. It isn't used in Java programs so often but it is quite commong for command line utilities to take arguments e.g.
In this case the MyClass.java is an argument.
javac MyClass.java
Or like the following has three arguments.
java -cp . MyClass
This is, more or less, correct. Every whitespace-separated word that comes after java Program is stored into an array of Strings, which happens to be called args.
An example on how to use this for your benefit:
public class Test {
public static void main(String[] args)
{
if(args.length > 0)
{
System.out.println(args[0] + "\n");
}
}
}
Compile this with:
> javac Test.java
And then run it:
> java Test Yes
"Yes" is then printed to your screen.
This question already has answers here:
Difference in System. exit(0) , System.exit(-1), System.exit(1 ) in Java
(12 answers)
Closed 7 years ago.
Can any one share to me difference between System.exit(0) and System.exit(-1) it is helpful if you explain with example.
It's just the difference in terms of the exit code of the process. So if anything was going to take action based on the exit code (where 0 is typically success, and non-zero usually indicates an error) you can control what they see.
As an example, take this tiny Java program, which uses the number of command line arguments as the exit code:
public class Test {
public static void main(String[] args) throws Exception {
System.exit(args.length);
}
}
Now running it from a bash shell, where && means "execute the second command if the first one is successful" we can have:
~ $ java Test && echo Success!
Success!
~ $ java Test boom && echo Success!
System.exit(0) means it is a normal exit from a program.But System.exit(-1) means the exit may be due to some error. Any number other that zero means abnormal exit.
The parameter of System.exit(int) is the return value of your program, which can be evaluated in batch jobs (usually for console programs). By convention, every value other than 0 inidaces that something went wrong.
If you run your Java program in Linux/Unix you can examine the result using echo $? command. This is why it is important to call System.exit(0) (this is done for you if you don't) if everything is fine and System.exit(non-zero) otherwise.