简体   繁体   English

如何向Java应用程序添加参数

[英]How to add arguments to a Java application

I want to add arguments to my Java application before i run it. 我想在运行Java应用程序之前为其添加参数。 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. 我想做一些事情,例如当您单击Word文档时,它会自行打开文档,您不必先打开word,然后单击“打开”。 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. 不确定要使用Word做什么,但我希望这会有所帮助。

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). 要将程序与文件扩展名相关联,以便自动调用该文件扩展名,您必须配置桌面环境(Linux)或Windows(Windows)(对于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 我没有头脑,但据我所知,您将扩展名(例如xtx)与启动命令结合使用,例如

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%: 如果您有或可以有更多的参数(标记多个文件,并将其拖到启动器中),则可以提高%9%:

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

%1% is for the first param and so on. %1%用于第一个参数,依此类推。 There is nothing you can do from Java, except catching those parameter, what you already do. 除了捕获这些参数(您已经完成的工作)之外,Java无法做任何事情。

On Linux, your starter is very similar: 在Linux上,启动器非常相似:

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

Java Web Start 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.. ..JWS提供了许多吸引人的功能,包括但不限于初始屏幕,桌面集成, 文件关联 ,自动更新(包括延迟下载和更新的程序控制),按平台,体系结构或Java版本对本机和其他资源下载进行分区,运行时环境的配置(最低J2SE版本,运行时选项,RAM等),使用扩展轻松管理公共资源。

For a demo. 进行演示。 of the file associations, see the JNLP API file service demo . 有关文件关联的信息,请参阅JNLP API文件服务演示

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. 您尝试创建的应用程序是GUI应用程序,而main方法采用的参数用于命令行界面输入。

Where string [] args is the String array that can story many arguments from CLI. 其中string [] args是可以讲述CLI中许多参数的String数组。 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. 当然,您可以通过CLI和GUI程序的组合来实现该功能,但是将只能从CLI启动应用程序,这没有任何意义。

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. 这样,您就可以拥有完整的GUI应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在eclipse java应用程序中添加java参数和classpath? - How to add java arguments as well as classpath in eclipse java application? 如何在Java中的SOAP Web服务中添加多个参数 - How to add Multiple Arguments In SOAP Webservice in java Java:如何添加或创建带有参数的新对象并将其添加到队列中? - Java: How to add or create a new object with arguments and add it to a queue? 如何在Mac OS X上打开带有参数的Java应用程序? - How to open a Java application with arguments on Mac OS X? 如何将参数从Java应用程序传递到Windows时间计划程序? - How to pass arguments from a java application to a windows Time Scheduler? 如何将“不安全”参数传递给Java Webstart应用程序的JVM - How to pass 'unsafe' arguments to the JVM of a Java Webstart application 如何运行将图像作为参数在java中的应用程序的批处理文件 - How to run a batch file of an application that takes images as arguments in java 如何从Java应用程序内部获取VM参数? - How to get VM arguments from inside of Java application? 如何使用内置命令行参数运行Java应用程序? - How to run java application with built-in command line arguments? 如何在应用程序模式下将 java arguments 传递给 Flink 作业工件 - How to pass java arguments to Flink job artifacts in Application Mode
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM