Run java (eclipse) in another environment

1

I have made a program with eclipse (without graphic interface) and I would like to be able to run it outside the program, for example on the console, or with the aesthetics of the console. Similar to when you program with the codeblocks and the executable creates you. The fact is that I have found videos and posts that comment on being able to run from a console but none of them has worked for me. In addition, the program requires interaction with the user and with files.

    
asked by mileswiles 09.12.2016 в 14:12
source

2 answers

0

@ Error404 is right, but if an application has been created, there may be several scenarios in which more needs to be done:

  • There is a class without a package
  • There is a class inside package (s)
  • There are multiple classes
  • There are multiple classes in multiple packages.
  • Depending on the scenario, the way this can be done varies somewhat:

    Assuming that JAVA_HOME has been added to environment variables and that the program called "ProgramX" and is in "C: //"

    1

    If there is only one class:

    The console opens and goes to the location of the program:

    WINDOWS+R >> cmd >> ENTER >> cd C://ProgramaX/src/ >> ENTER

    Your location should show you the file.java if you type dir >> ENTER .

    Then to run the program:

    javac ClaseUnica.java & java ClaseUnica >> ENTER
    

    2

    If there is a package of for example src/paquete/ClaseUnica.java then we are located in the folder src/ and:

    javac paquete/ClaseUnica.java & java paquete.ClaseUnica >> ENTER
    

    3

    If there are multiple classes then you have to compile all the classes first and then execute the class that has the main method:

    javac paquete/*.java >> ENTER
    java paquete.ClasePrincipal >> ENTER
    

    4

    If there are multiple classes in multiple packages, you can also use the 3 option, but this becomes very tedious. It would be best to create a JAR and then simply run it from the console. (this option works for all previous ones)

    Right click on the project in eclipse, click on the option export , write in the filter "runn", Runnable JARfile , click on this option and click on Next , in Launch Configurations , Select the class that has the main method, and select as export destination C://ProyectoX/src/ and so that we do not have to move from where we are in the console, click on Finish .

    Then from the console if you type dir >> ENTER you should see the new Archivo.jar .

    To execute it:

    java -jar NombreEjecutable.jar >> ENTER
    

    There are other ways to perform these tasks that is using a building tool such as ANT but this already has a learning curve more big.

    Edit

    How to add JAVA_HOME to environment variables so that the error

      

    javac is not recognized as an internal or external command ....

    Disappear:

  • WINDOWS + r
  • Type: "sysdm.cpl" ENTER
  • Select "Advanced Options" tab
  • Click on "Environment variables"
  • Click on "new" system variable
  • Name of the variable: "JAVA_HOME"
  • variable value: "C: //location/java/jdk1.x.x/"
  • In my case it was: "C: \ Program Files \ Java \ jdk1.8.0_112 \"

  • Find the system variable ## Path ##

  • Click on edit (DO NOT ERASE ANYTHING WHAT IS ALREADY)

  • At the end of the value of the variable, add a ";" if it does NOT exist

  • Add "% JAVA_HOME% / bin"

  • Accept, accept etc ...

  • answered by 09.12.2016 в 15:31
    0

    One option that you have is to generate the .jar of the program. In eclipse, press the right button on your project and press "Export ...". A window with several options will open; Display the Java option, select Runnable JAR file and click Next.

    In the next window, in the "Launch configuration" dropdown you choose the class of your project that you want to run the program (the class that contains the main function) and choose the path where you want your JAR file to be exported.

    Once this is done, you open the console and place yourself in the directory that contains the JAR file and execute "java -jar miprograma.jar". From there your program will run as it does eclipse on your console.

    You can also execute the previous command by entering the path of the file.

        
    answered by 10.12.2016 в 10:41