创建批处理文件以运行 Java 程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8277965/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-15 00:35:19  来源:igfitidea点击:

Create Batch File To Run Java Program

javabatch-file

提问by Mohamad Ibrahim

I want to create a batch file to run java project directly. I want this batch file to ask me to prompt inputs to be used in the run time for my project.

我想创建一个批处理文件来直接运行java项目。我希望这个批处理文件要求我提示在我的项目的运行时使用的输入。

采纳答案by GETah

The batch file should contain the following line:

批处理文件应包含以下行:

java MyProgram param1 param2...

and that's it. When started, your program can prompt the user for parameters using Scanner, see examplefor help.

就是这样。启动时,您的程序可以使用 Scanner 提示用户输入参数,请参阅示例以获取帮助。

回答by arcy

I agree with the previous answer that it makes more sense to have your java program request input instead of attempting to do it in the batch program.

我同意之前的答案,即让您的 java 程序请求输入而不是尝试在批处理程序中输入更有意义。

You don't say what operating system(s) you want to run this on, and that will make some difference to the batch file or script file.

您没有说明要在什么操作系统上运行它,这将对批处理文件或脚本文件产生一些影响。

Other things that will make a difference are the run-time environment on the system and whether the "default directory" is significant.

其他会产生影响的事情是系统上的运行时环境以及“默认目录”是否重要。

To invoke the java runtime, the system must recognize 'java' as a program name. On Windows and Unix (and therefore, I think, on Mac), this is done with a 'path' variable. The path is a "system variable", available to the command-line processor, that lists the directories in which the system will search for commands. For all system users to be able to use "java" on a command line, the path will need to be set globally, and your script cannot do that; you'll have to find out how to set it outside the script.

要调用 java 运行时,系统必须将“java”识别为程序名称。在 Windows 和 Unix 上(因此,我认为,在 Mac 上),这是通过“路径”变量完成的。路径是一个“系统变量”,可供命令行处理器使用,它列出了系统将在其中搜索命令的目录。为了让所有系统用户都能够在命令行上使用“java”,需要全局设置路径,而您的脚本不能这样做;你必须找出如何在脚本之外设置它。

Alternately, your script could invoke java from its absolute location on the system, but of course different computers could have different values. Requiring that the path be set correctly on a given system for the script to run is probably the most common answer, but you should be aware of the problem.

或者,您的脚本可以从其在系统上的绝对位置调用 java,但当然不同的计算机可能具有不同的值。要求在给定系统上正确设置路径以运行脚本可能是最常见的答案,但您应该意识到这个问题。

Most systems have the concept of a "default directory" for a running program. If your java program opens the file "MyData.txt", then the java runtime will look for this file in the default directory; if it opens "..\MyData.txt", it will look in a sibling directory to the default on a Windows system (note backslash), etc. It can open files on an absolute path, though those tend to be system-specific. So hopefully your program is either expected to run on only one kind of system, or handles files in a general way, but if you're just starting out writing a script to run your program you should be aware of the issue. There's little that's more frustrating than a program that runs fine the way you have it set up for development but refuses to run in any other environment, especially if you aren't aware of these potential pitfalls.

大多数系统都有运行程序的“默认目录”的概念。如果你的java程序打开了“MyData.txt”文件,那么java运行时会在默认目录中寻找这个文件;如果它打开“..\MyData.txt”,它将在 Windows 系统上的默认兄弟目录中查找(注意反斜杠)等。它可以在绝对路径上打开文件,尽管这些文件往往是特定于系统的. 所以希望您的程序要么只在一种系统上运行,要么以一般方式处理文件,但如果您刚刚开始编写脚本来运行您的程序,您应该意识到这个问题。没有什么比程序按照您为开发设置的方式运行良好但拒绝在任何其他环境中运行更令人沮丧的了,尤其是如果您不是

You also don't say whether the program is primarily a UI program (one that has a point-and-click interface) or not; you should also be aware that there are ways to package such a program so that it can appear on a system as an icon that can be run by clicking on it with a mouse.

您也没有说明该程序是否主要是一个 UI 程序(具有点击式界面的程序);您还应该知道有一些方法可以打包这样的程序,以便它可以作为图标出现在系统上,可以通过用鼠标单击它来运行。