How to compile a .java file using the ubuntu terminal

6

I am a beginner and need to compile a .java file using the ubuntu terminal, how to proceed?

    
asked by anonymous 16.09.2015 / 15:55

3 answers

5

With the Java SE Development Kit (JDK) installed, javac, which is a compiler for the Java language, can be used.

To compile the java code, the command is as follows:

$ javac SeuCodigo.java 

After executing the above command, if you do not enter NOTHING on your terminal it is because the compilation was executed successfully. Subsequently, it may be noted that a file was generated in the directory where your code was compiled.

To run it, run the command below:

$ java SeuCodigo

This generated file is nothing more than your compiled java program.

    
16.09.2015 / 16:04
5

To compile a .java file, open the termia and run the following command

javac arquivo.java

And to run the generated class file, use

java arquivo
    
16.09.2015 / 16:02
1

At what point are you? Have you already installed the Java Development Kit (JDK)? This kit has javaC which is the javaCompiler (program that will compile your .java files). If possible, let's go ahead, otherwise download and install JDK.

Onwards: Have you created the file with the .java extension? You can create it with a simple text editor by saving it as ClassName.java (hope that at this point, you know the basic structure of a class, otherwise the problem is before the compilation itself).

With the classname.java file created, open your terminal and go through the folders until you reach the location of this file (I think in Ubunto you can right-click anywhere in the open folder and put open in the terminal). Then you execute the command javac ClassName.java if there is no error found by the compiler, the terminal will jump to a new line waiting for the execution of any new command. At this point, if everything happened successfully, the file ClassName.class was created to run your program. You should now use the java command ClassName (only without extension at all).

I hope I have helped!

    
16.09.2015 / 16:34