How to compile a. java file using the Ubuntu terminal

Asked

Viewed 22,225 times

7

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

3 answers

6


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

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

$ javac SeuCodigo.java 

After running the above command, if NOTHING is reported on your terminal it is because the build has been successfully executed. Later, you may notice 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.

  • You need to be in the same directory or at least indicate the file path.

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

2

When are you ? Have you installed the JDK(Java Development Kit)? This kit has javac which is javaCompiler (a program that will compile your .java files). If possible, go ahead, otherwise download and install the JDK.

Forward: Have you already created the file with the . java ? You can create it with a simple text editor by saving it as Nomedaclasse.java (hopefully at this point, you know the basic structure of a class, otherwise the problem is before the build itself).

With the Filename.java created, open your terminal and walk through the folders until you get to 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 run the command javac Nameglass.java if there is no error found by the compiler, the terminal jumps to a new line awaiting the execution of any new command. At this point if everything happened successfully, the Classname.class file was created to run your program and you should now use the java Classname command (only no extension at all).

I hope I’ve helped!

  • 1

    Hello, in case the college machines already have the JDK, but if you can explain how to install the JDK via command line would be great.

  • 1

    Detailed information on the installation of the JDK can be found at this link: https://www.digitalocean.com/community/tutorials/como-instalar-o-java-no-ubuntu-com-apt-get-pt

Browser other questions tagged

You are not signed in. Login or sign up in order to post.