可执行的Jar文件

时间:2014-02-14 16:07:34

标签: java eclipse

如何让这些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();
    }
}
}

5 个答案:

答案 0 :(得分:1)

在Eclipse中,转到文件&gt;导出&gt; java&gt; Runnable jar文件&gt;启动conf pick主类。出口目的地。导出后的文件目的地&gt;完成

之后你可以用命令行运行jar(所以你可以看到你的控制台)在搜索中键入cmd并进入你的jar所在的目录运行jar ..运行jar文件的命令 java - jar nameofjar.jar

答案 1 :(得分:0)

已经从内存中写了这个,因为我现在无法访问eclipse。

右键单击项目

导出

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

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

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

选择文件位置和文件名

点击完成/完成

浏览你的jar,运行它

答案 2 :(得分:0)

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

第一原则:

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

答案 3 :(得分:0)

您始终可以从命令行

运行它

答案 4 :(得分:0)

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

此外,虽然与Eclipse IDE无关,但如果你想试验netbeans IDE这个过程很容易(Run - &gt; Clean and Build Project)或 Shift + F11 自动创建项目的可执行jar文件,位于dist目录下。