【问题标题】:Executable Jar files可执行 Jar 文件
【发布时间】:2014-02-14 16:07:34
【问题描述】:

如何让这个 Java 代码在 Eclipse 之外运行?

import java.util.*; 


public class Calculations {
public static void main(String[] args) {
    Scanner console = new Scanner (System.in); 

    System.out.println("So... you want to figure out what the Hypotenuse of any Right Triangle is but your to");
    System.out.println("lazy to do it your self huh..Sigh... Ok well, At least your being honest about it :)");
    System.out.println();
    System.out.println("Lets do this...Pythagoreom Style");
    System.out.println();
    System.out.println("First things first, whats your Sine? And no I don't mean if your an Aquarious or");
    System.out.print("some shit like that, I mean your Y value or the first side of your triangle?   ");
    double side1 = console.nextDouble();
    System.out.println();
    System.out.println("Okayyyy and now as any good math teacher would say, Whats your Cosine or X value?");
    System.out.print("(You might as well learn some Trigonometry while your taking the easy way out)   ");
    double side2 = console.nextDouble();
    System.out.println();
    System.out.println("Ok give me a minute...  ");
    System.out.println();
    System.out.println();
    System.out.print("Oh, by the way whats your favorite food?   ");
    String x = console.next();
    System.out.println();
    System.out.println();
    double hypotonuse = Math.sqrt((Math.pow(side1,2)+ Math.pow(side2,2))); 
    System.out.println("So you favorite food is " + x + " Huh...well...THATS BESIDES THE POINT!!! BACK TO MATH!!!");
    System.out.println();
    double sum = (Math.pow(side1,2)+Math.pow(side2,2)); 
    System.out.println("So if your First Side is "+side1 + " and we square that and your Second Side is " + side2+ " and we square that too,");
    System.out.println();
    System.out.println("we can add those sides together to get our sum of "+sum+" Finally we can square root "+sum+ " and Viola!");
    System.out.println();
    System.out.println("Pythagoras says your Hypotenuse is "+ hypotonuse);
    System.out.println();
    System.out.println();
    System.out.println("HHHAAAZZZAAAAA!!!! FOR MATH!!! HAAAZZZAAAA for Pythagoreum!!!");
    System.out.println();
    System.out.println();
    System.out.println("Oh, and P.S.");
    System.out.println();
    System.out.println("I'm just kidding, I care about your favorite food,");
    System.out.println();
    System.out.println("Here's a pyramid type thing of it built by a ratio given by your Hypotenuse :)");
    System.out.println();
    System.out.println();
    for( int i =1; i <=hypotonuse; i++)
    { 
        for (int w = 1; w<=(hypotonuse-i); w++)
        {
            System.out.print(" ");
        }

            for(int v=1; v<= (2*i-1); v++)
            {
                System.out.print(" "+ x );
            }

        System.out.println();
    }
}
}

【问题讨论】:

标签: java eclipse


【解决方案1】:

在 Eclipse 中转到 File>Export>java>Runnable jar file>Launch conf 选择主类。出口目的地导出后文件的目的地>完成

之后你可以使用命令行运行jar(这样你就可以看到你的控制台)在搜索中输入cmd并通过进入你的jar所在的目录来运行jar ..用于运行jar文件的命令java - jar nameofjar.jar

【讨论】:

    【解决方案2】:

    虽然您可以从 Eclipse 中导出该类并运行它,但我建议您从第一原则开始执行它以了解该过程。稍后在您了解基础知识并处理复杂应用程序后,您可以使用 ant 或 maven 等构建工具生成 jar 文件并运行它。

    第一原理方式:

    1. 确保您可以从命令行运行 java 编译器和 JVM。您应该将您的 JAVA_HOME/bin 添加到 PATH 以使其正常工作。
    2. 打开命令提示符。 cd 到您拥有 Calculations.java 文件的目录。
    3. 通过发出命令javac Calculations.java编译java代码
    4. 编译器在目录中生成Calculations.class文件。
    5. 您可以通过发出命令java Calculations 在Eclipse 外部运行已编译的类。是的,您没有在命令中使用.class 扩展名。
    6. 如果您在一个更大的项目中有多个类文件,您可以使用命令jar -cvf calculations.jar * 将它们打包到一个 jar 文件中
    7. 它会生成包含编译代码的calculation.jar 文件。
    8. 您可以通过发出命令java -jar calculations.jar Calculations 来运行 jar 中的代码

    【讨论】:

      【解决方案3】:

      这是凭记忆写的,因为我现在无法访问 eclipse。

      右击项目

      导出

      可执行/可运行的 jar 文件。

      (接下来的两个步骤可能顺序错误)

      确保为 main 方法选择正确的类。

      选择文件位置和文件名

      点击完成/完成

      浏览到你的 jar,运行它

      【讨论】:

      • 我还要补充一点,在这种特殊情况下,OP 应该从命令行运行 jar,否则在执行后控制台会突然消失而不会让用户看到结果
      • 好点。或者他们可以添加请求用户按键退出并添加另一个 console.nextxxx();
      【解决方案4】:

      你总是可以从命令行运行它

      【讨论】:

        【解决方案5】:

        herehere 在 stackoverflow 上的回答非常详细。
        此外,您正在尝试从 jar 运行“控制台”应用程序(如果您执行 jar,通常不会显示任何内容)。您可以按照these 的说明操作。

        此外,虽然与 Eclipse IDE 无关,但如果你想试验 netbeans IDE,这个过程在那儿很容易(运行 --> 清理并构建项目)或 Shift + F11 自动创建一个项目的可执行jar文件,位于dist目录下。

        【讨论】:

          猜你喜欢
          • 2012-05-04
          • 2017-01-23
          • 2015-07-25
          • 1970-01-01
          • 2012-04-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-08-14
          相关资源
          最近更新 更多