Running Java Programs¶
Table of Contents¶
- Overview
- Running Java Programs
- Example: Running
HelloWorld.java - Best Practices
- Maven Commands Cheat Sheet
- POM File Structure
Overview¶
This guide explains how to run Java programs using the command line, an IDE, or a build tool. It includes an example use case, best practices, and commonly used Maven commands for Java projects.
Running Java Programs¶
Using the Command Line¶
To run a Java program from the command line:
- Open a terminal or command prompt.
- Navigate to the directory containing your Java file.
- Compile the program using the
javaccommand:(Replacejavac MyClass.javaMyClasswith your Java class name.) - Execute the program using the
javacommand:java MyClass
Using an IDE¶
Follow these steps to run Java programs in an IDE:
- Open the IDE and create a new project.
- Add a new Java class and write your code.
- Compile the program by clicking Compile or using the IDE's shortcut.
- Run the program by clicking Run or using the appropriate shortcut.
Using a Build Tool¶
Steps to run Java programs with tools like Maven or Gradle:
- Set up a new project in your build tool.
- Add your Java code to the project directory.
- Compile the program using the tool's command (e.g.,
mvn clean compilefor Maven). - Run the program using the tool's command (e.g.,
mvn exec:javaorgradle run).
Example: Running HelloWorld.java¶
Suppose you have a program HelloWorld.java that prints "Hello, World!" to the console. Here’s how to run it:
- Open a terminal.
- Navigate to the directory containing the file.
- Compile the file:
javac HelloWorld.java - Execute the program:
java HelloWorld
Best Practices¶
- Compilation: Always compile your Java program before running it.
- Tools: Use IDEs and build tools to simplify the compilation and execution process.
- Testing: Test thoroughly using unit and integration tests.
- Version Control: Track changes with Git or other version control systems.
- Automation: Set up CI/CD pipelines to automate builds and deployments.
- Analysis: Use code analysis and formatting tools to ensure quality and consistency.
- Code Reviews: Conduct code reviews to enhance maintainability.
- Mocking: Use mocking frameworks (e.g., Mockito) to write isolated tests.
Maven Commands Cheat Sheet¶
Here’s a list of essential Maven commands for managing Java projects:
# Install Maven
sudo apt update
sudo apt install -y maven
# Verify Maven installation
mvn -v
# Compile the project
mvn clean compile
# Run tests
mvn clean test
# Package the project
mvn clean package
# Skip tests while packaging
mvn package -DskipTests=true
# Use Maven Wrapper for packaging
./mvnw package
POM File Structure¶
<!--
Root element defining the project configuration and metadata.
The 'project' element is required and adheres to the Maven POM (Project Object Model) standard.
The namespace and schema details ensure the XML conforms to the Maven specification.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<!--
Model Version: Specifies the version of the POM model used.
For Maven 2.x and 3.x, this value should always be 4.0.0.
-->
<modelVersion>4.0.0</modelVersion>
<!--
Group ID: Specifies the unique identifier for the project's group or organization.
Typically represents a domain or company name in reverse format.
-->
<groupId>com.example</groupId>
<!--
Artifact ID: The unique name of the project or module.
Used to identify the artifact within the group.
-->
<artifactId>bankapp</artifactId>
<!--
Version: Specifies the version of the project.
- Use 'SNAPSHOT' in the version (e.g., 0.0.1-SNAPSHOT) to publish to the snapshot repository.
- Use a specific version number (e.g., 1.0.0) to publish to the release repository.
This decision determines whether the artifact is considered a development version (SNAPSHOT)
or a stable release version.
-->
<version>0.0.1-SNAPSHOT</version>
<!--
Name: A human-readable name for the project.
This is purely for informational purposes and does not affect repository selection.
-->
<name>IbtisamX Bankapp</name>
<!--
Description: A brief description providing details about the project.
This is useful for documentation or repository indexing.
-->
<description>Banking Web Application</description>
<dependencies>
<!-- Add your dependencies here -->
</dependencies>
<build>
<plugins>
<!-- Add your plugins here -->
</plugins>
</build>
<!-- Other project information -->
<!--
Distribution Management: Specifies the locations where the project's artifacts should be deployed.
This section is crucial for configuring Nexus repositories for snapshots and releases.
-->
<distributionManagement>
<!--
Repository: Used for deploying release versions of the project.
Artifacts with versions like '1.0.0' will be deployed here.
-->
<repository>
<id>maven-releases</id> <!-- ID for the release repository, referenced in settings.xml -->
<url>NEXUS-URL/repository/maven-releases/</url> <!-- URL of the release repository in Nexus -->
</repository>
<!--
Snapshot Repository: Used for deploying development versions (e.g., 1.0.0-SNAPSHOT).
Artifacts with versions ending in '-SNAPSHOT' will be deployed here.
-->
<snapshotRepository>
<id>maven-snapshots</id> <!-- ID for the snapshot repository, referenced in settings.xml -->
<url>NEXUS-URL/repository/maven-snapshots/</url> <!-- URL of the snapshot repository in Nexus -->
</snapshotRepository>
</distributionManagement>
<!-- Other project configuration -->
</project>