Within the folder, it creates the Source folder. It instantly creates the myJavaProgram folder. The_Directory_Where_I_Have_Kept_My_Source_Code.This will create a folder named myJavaProgram, then create another folder named Source under the myJavaProgram folder under the directory where my source file is now. file_name_with_the_extension.Īs for now, I am using the Main.java file, so I will apply the command javac -d. The directory tree looks like this: myJavaProgram > Source.įor compiling this type of Java code with the packages, we use the command javac -d. Finally, I want to create the class file of my Java code inside the Source folder. Then, I want to create another folder under that named Source. This indicates that I want to create a folder named myJavaProgram. In the first line, I have written the package as package myJavaProgram.Source. This time, I will be using the following Java code. Now I will show you how you can run any Java code that has packages declared within it. There, I did not use any packages inside the Java code. Earlier, I showed you how to use any regular Java code using the terminal. in Linux OS □Bonus: How to Run a Java Program with PackagesĪ package basically means a folder. The exact same procedure is also applicable for the other operating systems as well. The Java program has been executed successfully! class file for this is Main.class, our command will be java Main. For that, we use the command java class_file_name_without_the_extension. The same process is applicable for all of the operating systems out there. Keep in mind that we run the class file, not the. This will create the class file we need under the same directory. If the compilation process is successful, then we will not get any errors. For example, as I want to compile my Main.java, I will use the command javac Main.java. We need to use the command javac file_name_with_the_extension. How to compile Java code using the terminal Then we need to execute/run the class file. To compile a Java code/program, we get the class file. using the typical GUI way Using the cd command □□How to Compile the Java Codeīefore running our Java code, we need to compile it first. You can go into the directory where you have kept your source code either by following the typical GUI way or from the terminal using the cd command as well. How to go into the directory where the source code is: for Linux □ I get my Java file in the terminal as well. So I went in that directory using the cd command. I placed the Main.java file under my D drive this time. Then if I check all the files under that directory, then I will get the Main.java file as well. As my user name contains white spaces, I have used " " to enclose them. I need to use cd to indicate that I want to change directory. Or, if I want, I can also go there using my terminal. I can simply go there through my file explorer. Suppose I have the source code ( Main.java ) inside This PC > Documents folder. How to go into the directory where the source code is: for Windows □ You can go straight into the directory through your file manager if you want. If you have already written your Java code in an editor, then simply go into that directory. public class Main □ Step 1 – Go to the directory where your source code is I will be using the following Java code in the next step. The process I am going to show you in this article is applicable to any operating system whether that is Windows, MacOS, or Linux. Fear not! The procedure is quite easy, and after reading the entire article you should be able to run your own Java code in the terminal. In this article, I will show you how you can execute Java directly from your favorite terminal window. Whatever your purpose may be, executing Java code directly from the terminal is a very easy task. This gives them better clarity and helps them understand how the code is working, where it is returning the desired value, where the bug might be, and so on. Many of you might have heard that experienced professional programmers also use the terminal to execute the programs. However, it is good practice to know how your code actually executes and provides the output it gives you. Running code in your IDE is straightforward, but you don't often get to see how it executes your code (even though you can check the command in the terminal of course!). If you work with Java, you've probably used one of the well known text editors like Sublime Text, VS Code, Brackets, Atom, and Notepad++ as well as IDEs like Apache NetBeans and IntelliJ IDEA.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |