@ 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 ...