简体   繁体   中英

How to add arguments to a Java application

I want to add arguments to my Java application before i run it. I want to be able do somthing like:

public static void main(String args[])
{
    String document = args[0];
    new DocumentViewer(document);
}

I want to do somthing like when you click on a Word document it opens up the document by itself, you dont have to open word and then click open. Does anyone know how to add arguments? All relevant answers are appriciated!

If you're launching your app from the command line you could just pass the arguments separated by a blank space right after the application name like this:

java name_app arg1 arg2 etc...
//the code above passes to name_app 3 strings: "arg1", "arg2", and "etc..."

Not sure about what you want to do with Word but I hope this was helpful.

java YourClass yourfile.xtx

To associate your program with a file extension, so that it is automatically called, you have to configure your Desktop Environment (Linux) or Windows (Windows) (I don't know for OSX).

I don't have it in my head, but as far as I remember, you combine the extension, xtx for example, with a starting command, like

java -cp C:\Programs\yourlibs\your.jar YourClass %1%

If you have or can have more arguments (mark multiple files, and drag them to your starter) you can, afaik, go up to %9%:

java -cp C:\Programs\yourlibs\your.jar YourClass %1% %2% %3% 

%1% is for the first param and so on. There is nothing you can do from Java, except catching those parameter, what you already do.

On Linux, your starter is very similar:

java -cp /usr/local/lib/your.jar YourClass $1 $2 $3

Java Web Start

..JWS provides many appealing features including, but not limited to, splash screens, desktop integration, file associations , automatic update (including lazy downloads and programmatic control of updates), partitioning of natives & other resource downloads by platform, architecture or Java version, configuration of run-time environment (minimum J2SE version, run-time options, RAM etc.), easy management of common resources using extensions..

For a demo. of the file associations, see the JNLP API file service demo .

The application that you are trying to create is a GUI application while the arguments that main method take is meant for Command Line interface input.

Where string [] args is the String array that can story many arguments from CLI. Of course you can implement the feature with a mix of CLI and GUI program but then you will be limited to launch your application from CLI, which doesn't make sense.

Another workaround can be. Create a demo frame that appears in the beginning have some textboxes and let users enter what you want to take as an argument, pass that value in the method, or data types you like. In that way you can have a full blown GUI application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM