How to Create a Java Project in Eclipse

Eclipse helps to clearly manage the project structure, allowing for systematic organization of multiple files and folders.

 

Step 1 > Create Project

Eclipse, Java project
Eclipse, Java project
Eclipse, Java project
Eclipse, Java project
Eclipse, Java project
Eclipse, Java 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’

Eclipse, Java project
Eclipse, Java project

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.

Eclipse, Java project
Eclipse, Java project
Eclipse, Java project
Eclipse, Java project
Eclipse, Java project
Eclipse, Java project
  • 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.
Eclipse, Java project
Eclipse, Java project
Eclipse, Java project
Eclipse, Java project

It is important to develop the habit of saving files after making modifications.

Eclipse, Java project
Eclipse, Java project

 

Step 3 > Execute Byte code

Eclipse, Java project
Eclipse, Java project

OR

Eclipse, Java project
Eclipse, Java project

Result

Eclipse, Java project
Eclipse, Java project

 

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.

Command, Java project
Command, Java project

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

Command, Java project
Command, Java project

 

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.

Command, Java project
Command, Java project

 

Copy the sec03 folder from the bin folder and paste it into C:\Temp.

Command, Java project
Command, Java project

 

Run the bytecode file using the java command.

java -cp [location of bytecode files] [package.package.bytecode filename]
java -cp . sec03.exam01.Hello

Command, Java project
Command, Java project

 

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

https://www.eclipse.org/