简体   繁体   English

将基于Java文本的程序导出到可执行JAR?

[英]Exporting a Java text-based program into an executable JAR?

I am using Eclipse as my IDE. 我正在使用Eclipse作为我的IDE。

I have previously made and exporting my different programs in eclipse as executable jar files, but those were all programs involving frames. 我以前已经在eclipse中制作并导出了我的不同程序作为可执行jar文件,但是这些都是涉及框架的程序。 I was messing around with the classic text based programs I had and decided that I wanted to export the said text-based programs. 我在弄乱我拥有的经典的基于文本的程序,并决定要导出所述基于文本的程序。 However, when I try and do this I am left with a jar file that does not run or do anything. 但是,当我尝试执行此操作时,剩下的jar文件无法运行或无法执行任何操作。 I have had no success in having my text-based programs to run outside of Eclipse. 让我的基于文本的程序在Eclipse之外运行没有成功。

The odds are that the jar file IS running. 很有可能jar文件正在运行。 But the problem is that (I'm assuming here) you're using System.out and System.in for your i/o. 但是问题是(我在这里假设)您正在将System.out和System.in用于您的I / O。 When you're running a jar file (with double-click ) you have no visible console. 在运行jar文件(双击)时,没有可见的控制台。

There are two ways to combat this: 有两种解决方法:

-Either create a gui and use that as your i/o -要么创建一个GUI并将其用作您的I / O

-Or you can run the jar file from the command line ("java -jar yourjar.jar" <- on windows cmd) and the input and output will be done through the command window you used to run the jar. -或者,您可以从命令行运行jar文件(在Windows cmd上为“ java -jar yourjar.jar” <-),输入和输出将通过用于运行jar的命令窗口来完成。

EDIT: Added some information about fixing: "java is not a recognized..." 编辑:添加了有关修复的一些信息:“ java不能识别...”

You have two options here, when you're running your jar with the command "java -jar yourjar.jar"; 使用命令“ java -jar yourjar.jar”运行jar时,这里有两个选择。 you could replace "java" with the full path to your java executable, so for example: 您可以使用Java可执行文件的完整路径替换“ java”,例如:

"C:\Program Files\Java\jdk1.8.0_05\bin\java" -jar yourjar.jar

or 要么

"C:\Program Files\Java\jdk1.8.0_05\jre\bin\java" -jar yourjar.jar

This does become annoying though, so the best things to do is set up java in your system environmental variables. 但是,这确实变得很烦人,因此最好的方法是在系统环境变量中设置java。 Open your 'Start Menu', right-click on 'Computer' and select 'Properties'. 打开“开始菜单”,右键单击“计算机”,然后选择“属性”。 Once you're here select 'Advanced System Settings', and then click on the 'Environmental Variables' button. 在这里选择“高级系统设置”,然后单击“环境变量”按钮。

Now what I like to do is create another two variables (if you don't plan on doing any of this on other users, just do it in your user-variables for your account section), "JDK_HOME" and "JAVA_HOME". 现在,我想做的是创建另外两个变量(如果您不打算对其他用户执行任何此操作,只需在帐户部分的用户变量中执行),即“ JDK_HOME”和“ JAVA_HOME”。 I personally have both of these point to the same place just because of a little error I was having with something. 我个人将这两个点都指向同一个地方,只是因为我在处理某些内容时遇到了一些错误。 However in your case it sounds like you only need one, we'll go with JDK_HOME: 但是,在您的情况下,听起来您只需要一个,我们将使用JDK_HOME:

So add your variable: 因此,添加您的变量:

在此处输入图片说明

And then we want to actually have it on the Systems Path, so look through the variables and find one called "PATH", edit this one and we're going to be adding the JDK_HOME variables directory and the bin folder to it: 然后,我们希望将其实际放置在系统路径上,因此仔细查看变量并找到一个名为“ PATH”的变量,对其进行编辑,然后将其添加JDK_HOME变量目录和bin文件夹:

在此处输入图片说明

When appending multiple directories to the Path variable, each are separated by a semi-colon. 将多个目录附加到Path变量时,每个目录都用分号分隔。 You'll note that I am adding the variable JDK_HOME by specifying %JDK_HOME%, but I am then adding '/bin' to it, this is because we are interested in using the Java executable within the bin directory. 您会注意到,我通过指定%JDK_HOME%添加了变量JDK_HOME,但是随后向其中添加了“ / bin”,这是因为我们对在bin目录中使用Java可执行文件感兴趣。 Now that you have that sorted, close out of your environmental variables configuration, and try again, you should now have the command "java" work. 既然已经进行了排序,请关闭环境变量配置,然后再试一次,现在应该可以使用命令“ java”了。

-Thomas -托马斯

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

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