I searched dozens of threads about this topic.
What I am looking for is to start a program using custom attributes, like:
java -C:/randof/...FROMPATH -C:/rudolf/...TOPATH
I want to start the program using the commandline (obviously) and directly passing arguments in it. The from and topath that are passed should be able to be used internally.
I cannot properly extract them though. If I iterate over "args", it brings me an error "Unrecognized option:". How is the thing I wanna achieve possible?
And is it only possible by passing "VM-Arguments"? This would mean that the arguments would be available over all java programs that run at the same time if not mistaken. I would like to enclose the scope if possible.
use -- before the arguments of your program:
java -C... -other-java-options -- -your-app-options etc
in the above command line java is the executable and all the rest are arguments that are passed by your shell to java. Java tries to interpret them. The common way to tell a program, that the arguments to it are ending here, and from here the arguments are meant for something else (in this case to your java code) so don't bother parsing them, just pass them on.
The primary way to get command line options to be recognized as options to the program you are writing and not as options to the java executable is to use defines -Dproperty=value and then you can pull these out using the System object. If you don't insist on having a leading dash, then the options will become elements in the String[] parameter passed to main.
Other -XXXX arguments are passed to the java executable (actually the -D is passed to the java executable too, but the java executable then makes it available to the program.
I think you shouldn't use "-" when you give the arguments.
Just write (if the compiled file has the name SofExample.class):
java SofExample C:/fromPath C:/toPath
and then in the program you will be able to use the arguments, because they will be in the args String array.
For example:
public class SofExample {
public static void main(String[] args) {
final String fromPath=args[0];
final String toPath=args[1];
//Args checks and other codes come here to use fromPath and toPath...
}
}
These arguments can be used only in this program code, not in all programs running in the JVM.
I hope I could help. Regards.
None of the answers did what I wanted (maybe my question was badly formulated), but I found the solution.
You can just add the parameters you need without a specific operator.
Consider the following main method:
public static void main(String...args) {
for (String a : args) {
System.out.println(a);
}
}
Here is the output for the following inputs:
java -jar YOURJARFILENAME.jar C:/Users/RUDOLF
//Output:
C:/Users/RUDOLF
java -jar YOURJARFILENAME.jar C:/abc C:/def
//Output:
C:/abc
C:/def
If you need to hand over filepaths that contain whitespaces, just add "" around every path. This will ensure that it is read as one argument and not be splitted up.
Problem: java -jar YOURJARFILENAME.jar C:/users/arnold schwarzenegger/desktop
//Output:
C:/users/arnold
chwarzenegger/desktop
Solution: java -jar YOURJARFILENAME.jar "C:/users/arnold schwarzenegger/desktop"
//Output:
C:/users/arnold schwarzenegger/desktop
Related
public static void main(String [] args)
This thing has been quite the enigma ever since I started programming. I understand it passes an array of Strings to the command line, but where exactly does it do that and how does it interpret it?
I'm pretty sure I'm wrong here, but my current understanding is that whatever you write in the main method is then translated into machine and passed into the computer as a string. Well, that's all fine and dandy, but then how does it understand numeric values if everything is a String?
Also, it's a bit weird when it comes to methods. Usually, when designing a method, you would write the formal parameters that you'd pass to it, and what you want to do with those parameters. But with the main method, you are defining the parameters while you're writing the code inside the main method! how does that even work?
The public static void main(String[] args) method in Java is the method that gets called when the user starts running your program. Inside this method, it is up to you to write whatever code (classes and methods) you need to make the program work.
Yes, parameters for this method are strings, but this is because when the program is executed from the command line, these args are the parameters that the user typed after the name of the class to execute. It is up to your code to parse them into numbers if that is what you require.
I'm pretty sure I'm wrong here, but my current understanding is that
whatever you write in the main method is then translated into machine
and passed into the computer as a string. Well, that's all fine and
dandy, but then how does it understand numeric values if everything is
a String?
This statement doesn't make particular sense. Yes, the class, which may contain the main method, is compiled to byte code, which can be read/interpreted by the JVM and executed on the machine.
When the JVM is executed (by running the java command) it looks for the main method in the specified class (or if you are executing a jar file, the class specified by the Main-Class manifest entry). If found, it will pass the command line parameters as a String array to your main method.
Also, it's a bit weird when it comes to methods. Usually, when
designing a method, you would write the formal parameters that you'd
pass to it, and what you want to do with those parameters. But with
the main method, you are defining the parameters while you're writing
the code inside the main method! how does that even work?
Because it's impossible for the JVM to know what parameters might be sent to it from the command line, it wraps all the parameters up within a String array, meaning that, you could have lots and lots of different parameters sent to your main method.
It is up to you to interpret these parameters based on the needs of your program and determine if they suitable enough for you to continue execution.
Take a look at:
Chapter 12 of the Java Language Specification: Execution
Lesson: A Closer Look at the "Hello World!" Application
for more details.
Historically, the main method comes from C/C++ as they also have a concept of a main function which operates in a similar fashion
I am not familiar with the internals of the JVM, but assuming it is running on a typical Linux/Unix systems, here are some possibilities:
1) The JVM itself is the actual program that is invoked giving it the the .class name as the input (containing the main method) along with additional args if needed - e.g.:
java SomeProgram input1 input2 .....
where SomeProgram.class is the compiled from SomeProgram.java which is the class that has the usual main() method.
2) Now the JVM in a typical Linux/Unix environment is a 'ELF binary' (executable and linking format) which has the 'main' method defined. All the gory details are described here including how the inputs are passed from a shell to a running executable (on a Intel platform):
http://linuxgazette.net/84/hawk.html
3) In short the JVM gets the inputs as described in (2), and then effectively runs the Someclass.main() method by copying all the inputs to the String[] array.
4) All inputs are treated as String, so if you need to treat some inputs as numeric, you would need to explicitly cast them as needed.
Although the ELF standard is part of Linux/Unix systems, I am assuming something similar is happening in other platforms supporting Java.
I am hoping someone with more understanding of JVM internals can correct me if I am wrong.
I tried to collect some information about the system such as memory usage and cpu usage and pass it to a java application which is responsible to store it into a database.
Well you are on the right path on calling the java program with at the end of your bash script.
I am 99.9% sure you need to call java -jar not just java in your bash script.
java Program $perName $systemCpuUsage $systemMemoryUsage $readingTime
java -jar Program $perName $systemCpuUsage $systemMemoryUsage $readingTime
Also in your main java class I think your should call a for each statement instead of a fixed loop.
for(String arg : args)
{
System.out.println(arg);
}
And the only other thing I can think of is that there is something wrong with your bash script is not getting the variables correctly. You should try to echo out your results to debug what the variables are supposed to be in the bash script.
EDIT
ArrayIndexOutOfBoundsException means that your looping two many times. Replacing it with a for each will tell you how many variables were passed through. When you get that error it means that your args array is not big enough and your trying to get data from a section in your array that doesn't exists.
I used the following in Java using Eclipse to run a program by the Runtime command below:
String[] cmd = { "C:\\Program Files\\Windows Live\\Messenger\\msnmsgr.exe",first,third };
notice that first,third are both String variables, now when this command runs the program I want to pass both of them into 2 textboxes. I want these string to be copied into the program i ran textboxes like here. only i need this one thing, how can i pass the strings, and why some of the users told me to use this code to pass the string variables, is it a pointer problem?
You simply cannot do that, unless you can pass the arguments in through the command line you have no way of interacting with a separate program's interface.
Whenever you declare the main method in a class, you always have to do a String array called "args". What's the point? Unless I live under a rock, command line agruments in Java are barely used anymore. And when I try and run this...
//this program won't compile
public class SomeClass {
public static void main(){
System.out.println("This text will never be displayed :(");
}
}
The output shows this in red text:
Error: Main method not found in class SomeClass, please define the main method as:
public static void main(String[] args)
I, the newbie Java programmer, would greatly appreciate it if anyone told my why it's required to enter that parameter into a main method.
Because that is the signature of the main method that is called when you execute a Java class. There needs to be some convention which method will be executed. By convention it is the
public static void main(String[] args) method
And yes, you do live under the rock, there are plenty of situations when command line arguments are used. Why would they not be used?
You could ask: why require it? Why not just pick any other main method? The answer is that it would be adding complexity with 0 benefit. As is now, main function looks distinctive. If you look at it, you know it is the one that will get called. If any main would be called, you would have to always ask yourself: is the main I am looking at the one to be invoked, or is there another main in this class which takes precedence?
Short answer: because that's the way Java is.
Command-line arguments are used all the time, but you don't always see them due to launcher scripts, or because the program's running on a server, etc.
That said, a lot of time the command line arguments are of the -D variety, slurped up by the JVM before reaching main. But it depends on what you're doing.
A Java application can accept any number of arguments from the command line. This allows the user to specify configuration information when the application is launched. (From Command-Line Arguments) and as everyone else said here, it is the way it is!
For gods sake,Please Don't say if I don't need this ,no-one else need this! :)
Because
Command-line arguments are still used, even by many UI programs (did you know that Microsoft Outlook supports some very handy command-line arguments?)*; and:
That's Just How Java Works (TM). Among other things, it reduces the complexity of both the code (by disallowing multiple forms and possible accidental shadowing) and run-time (by not needing to find out "which main" to call). Allowing a secondary form without "args" just adds too little.
Happy coding...
*Yes, Outlook is not Java. However, if Outlook has command-line arguments, well, they must still be worth something -- it was a hyperbole ;-)
Almost every UI program that deals opening reading files will allow specifying which file to open via command-line arguments (Gimp, Notepad, Firefox, to name a few others). Among other things, this is to allow integration with "double clicking to open" on items in Windows Explorer and similar.
I actually have no idea why it's required, other than to say that it is a syntactical convention. In the same way that a function is (defined function-name()) in Lisp/Scheme, or there are do..end blocks in Ruby, the syntax of a Java Main function is with String[] args.
As for not using command line arguments, it's entirely dependent on the program. Entirely. I write programs in java all the time that take command line arguments; it's just a question of what you're trying to accomplish.
One case I can think of is, when you want to have a command line driven interface for your software along with the GUI. An example is the Android tools, all of them have console driven interfaces.
Command-line argument support is largely standard among programming languages. Even in this age of GUIs, there are all sorts of hidden ways to run a program with command line arguments. I know Windows has shortcut configurations for advanced users where you can run a program with a given set of command line arguments, for example.
Java also enforces types, and by extension function signatures (look it up on Google if you don't know what those are). The main function is expected to take an array of Strings - if the main function you define doesn't match that argument signature (1 argument, array of Strings), then it causes an incompatibility.
Java supports function overloading (you can define the same function name several times with different arguments). To find which function to invoke, Java takes the input argument types and looks for an applicable defined function that takes the matching arguments.
When the program runs, Java specifically looks for a function named main with 1 argument (String []). Your program doesn't define a main function with that argument specification, so this lookup fails with an error message.
Since its possibly one of the most widely used methods of the Java language, why does it have to accept an array of Strings and doesn't work without it? For example, I could always live with:
public static void main() {}
over
public static void main(String[] args) {}
Is there a higher purpose to this than just being able to accept command-line arguments, especially since a vast majority of Java programs are GUI driven and don't need to receive args through command line?
I agree that it could be more flexible. In C# for instance, all of these are acceptable entry points:
static void Main()
static void Main(string[] args)
static int Main()
static int Main(string[] args)
The versions returning int use the return value as the exit code for the process. If you don't care about receiving any arguments, you can use one of the versions which doesn't take any. Any arguments given when the program is launched are ignored.
So yes, it could be more flexible - but it's never struck me as a significant problem in Java.
even a gui driven java app will start with some main method.
The "higher purpose" has never been to accept command line arguments.
The purpose is just to accept arguments. Period. Whenever you start any program not just Java you will always need some syntax to pass arguments
Programs always take commandline arguments. It's up to the programmer to decide if they are used for anything.
So implementing a main without a string-array would lead to more complex startup-logic and potentially confusing and errorneous behavior, for the more than debatable gain of not writing a single parameter declaration less (in your whole program!)
And given the overall verbosity of Java & the support for common templates for boilerplate code in IDEs like Eclipse, I fail to see where that's really an issue.
"Is there a higher purpose to this (..)"
Yes: legacy.
What "vast-majority" of programs do does not really make much difference in terms of what is needed to be done. There are still lots of command line programs that are driven by command line args.
Lots of GUI programs provide facilities to accept command-line switches to control their behaviour at start up.
I guess it's because many Java programs will take at least some parameters. Like C (a syntactical inspiration of Java if nothing else), Java considers the best way to provide these as parameters to main.
Even a GUI program will often take command line parameters. Just because the shell it's launched from typically doesn't ask for these parameters (by default), doesn't mean they're not supported.
there are also many java programs that receive args through command line. Think of javac, ant, maven, etc
A Java application can accept any number of arguments from the command line. This allows the user to specify configuration information when the application is launched. For example, we can pass username, password from the command line itself.
class Test {
static void connectDB(String username, String password) {
System.out.print(String.format("Username: %s, Password: %s", username, password));
// code to connect DB
}
public static void main(String args[]) {
connectDB(args[0], args[1]);
}
}
Now compile the program using javac Test.java
and Execute using java Test rootuser Test1234
The output will be:
Username: rootuser, Password: Test1234
So, we can use commandline-argument this kind of purposes.