0

How can I get this Java code to run outside of 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();
    }
}
}
Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • Read: [The Java™ Tutorials - Lesson: Packaging Programs in JAR Files](http://docs.oracle.com/javase/tutorial/deployment/jar/) and [Eclipse Help - Creating a New Runnable JAR File](http://help.eclipse.org/kepler/topic/org.eclipse.jdt.doc.user/tasks/tasks-37.htm). If you still have any doubts come back. – Anthony Accioly Feb 14 '14 at 16:13

5 Answers5

1

In Eclipse go to File>Export>java>Runnable jar file>Launch conf pick main class. export dest. destination of file after exporting >finish

after that you can run jar for example with command line(so you can see your console) type cmd in search and run jar by going into your directory where jar is located ..Command for running jar file java -jar nameofjar.jar

Tomas Bisciak
  • 2,801
  • 5
  • 33
  • 57
0

Have written this from memory, as I don't have access to eclipse right now.

Right click the project

Export

Executable/Runnable jar file.

(these next 2 steps may be in the wrong order)

Make sure you select the correct class for the main method.

Select the file location and file name

Click done/finish

Browse to your jar, run it

M21B8
  • 1,867
  • 10
  • 20
  • I would also add that in this particular case the OP should run the jar from command line, otherwise after execution the console would suddenly disappear without letting users see the results – BackSlash Feb 14 '14 at 16:15
  • Good point. Or alternative they could add in requesting the user presses a key to exit and adding another console.nextxxx(); – M21B8 Feb 14 '14 at 16:25
0

Though you can export the class from Eclipse and run it, I would recommend doing it from first principles to understand the process. Later after you understand the basics and work on complex applications, you can use a build tool like ant or maven to generate the jar file and run it.

First principles way:

  1. Ensure you can run java compiler and JVM from command line. You should add your JAVA_HOME/bin to PATH for that to work.
  2. Open command prompt. cd to the directory where you have the Calculations.java file.
  3. Compile the java code by issuing command javac Calculations.java
  4. Compiler generates Calculations.class file in the directory.
  5. You run the compiled class outside Eclipse by issuing command java Calculations. Yes, you don't use .class extension in the command.
  6. If you have multiple class files in a bigger project you can package them into a jar file using command jar -cvf calculations.jar *
  7. It generates calculations.jar file which contains compiled code.
  8. You run the code inside the jar by issuing command java -jar calculations.jar Calculations
RaviH
  • 3,544
  • 2
  • 15
  • 14
0

You could always run it from command line

NeonMedusa
  • 95
  • 2
  • 6
  • 14
0

It is answered here and here on stackoverflow with a great detail.
Also you are attempting to run a 'console' applicaiton from jar (which normally doesn't show anything if you execute the jar). You can follow these instructions about how to do it.

Also, though not related to Eclipse IDE, but if you want to experiment netbeans IDE this process is much easy there (Run --> Clean and Build Project) or Shift + F11 automatically creates an executable jar file for the project, located under dist directory.

Community
  • 1
  • 1
Abdullah Leghari
  • 2,332
  • 3
  • 24
  • 40