Eclipse helps to clearly manage the project structure, allowing for systematic organization of multiple files and folders.
Step 1 > Create Project
Unchecking ‘Create module-info.java file‘ will create a regular project instead of a modular project.
Step 2 > Create and Write Source File
Create a source file named Hello.java with the following code that prints ‘Hello, Java’
Java uses packages to easily manage source files and compiled bytecode files by functionality. A package is similar to a folder (directory) in a file system.
- Source folder: The folder where the package will be created.
- Package: The package that will contain the Java source files.
- Name: The name of the Java source file and the class to be written. It is customary to start with an uppercase letter.
- public static void main(String[] args): This must be included to run the Java source program after it is compiled.
It is important to develop the habit of saving files after making modifications.
Step 3 > Execute Byte code
OR
Result
Compile and Run from the Command Line
There are times when you need to compile and run the source files directly using the javac
and java
commands in the Command Line, such as Command Prompt or Terminal, instead of using the Eclipse development environment.
Open the Command Prompt and navigate to the chap01 folder.
Compile the source files in the src folder using the javac
command to generate bytecode files in the bin folder.
How to Use the javac
Command
javac -d [location to save bytecode files] [source path/*.java]
javac -d bin src/sec03/exam01/*.java
The -d
option specifies the folder where the bytecode files, including the package, will be stored. If there is only one source file, you can enter the file name directly, such as Hello.java
. If there are multiple source files, you can use *.java
To check if the bytecode files were generated after executing the command, run the following command: tree /f /a
If you want to execute in a Linux operating system, you can leave the source files in the Windows operating system and simply run them in Linux by copying the sec03
folder inside the bin
folder.
Create a Temp
folder on the C: drive, copy the sec03
folder into it, and then execute.
Copy the sec03
folder from the bin
folder and paste it into C:\Temp
.
Run the bytecode file using the java
command.
java -cp [location of bytecode files] [package.package.bytecode filename]
java -cp . sec03.exam01.Hello
The reasons for running Java in a command line or Linux environment include:
- Lightweight Environment: Command line and Linux environments are often less resource-intensive than graphical interfaces, allowing for faster execution of Java programs.
- Flexibility: It provides flexibility for developers who prefer working with scripts or automated processes, such as CI/CD pipelines.
- Remote Access: Many servers run on Linux, and being able to compile and run Java applications via command line allows developers to manage applications on remote systems.
- Testing and Debugging: Command line execution makes it easier to test and debug applications in various environments without the overhead of an IDE.
- Batch Processing: Running Java applications via scripts allows for batch processing and automation of tasks.
- Minimal Setup: There’s no need for a graphical interface, making it easier to set up environments on various systems.
More JAVA Informaion from Eunice0121…
https://eunice0121.com/category/java/
More Eclipse information..