How to compile and run Java Projects with java and javac commands

Compiling

-d is output directory for new generated class files.

How to compile single Java source file

javac -d ./target/classes src/main/java/com/extuni/enum_demo4/App.java

How to compile multiple Java source files

javac -d ./target/classes 
./src/main/java/com/extuni/enum_demo4/App.java 
./src/main/java/com/extuni/enum_demo4/Greeting.java 
./src/main/java/com/extuni/enum_demo4/Helper.java

How to compile multiple Java source files using wildcard

javac -d ./target/classes ./src/main/**/*.java

How to specify dependency jar file when compiling

Trying to compile ./src folder completely will throw exception due to junit.jar file dependency. So, it should be specified using -cp option which means –classpath as below.

javac -d ./target/classes -cp /Users/kenanhancer/.m2/repository/junit/junit/4.11/junit-4.11.jar ./src/**/*.java
Continue reading

How to create and run Java project with Maven

You can find more details in https://maven.apache.org/guides/getting-started/maven-in-five-minutes.html

1. Creating a Java Project with Maven

mvn archetype:generate 
    -DgroupId={project-package-name}
    -DartifactId={project-name}
    -DarchetypeArtifactId={maven-template-name}
    -DinteractiveMode=false
    -DarchetypeVersion=1.4
for example
mvn archetype:generate 
    -DgroupId=com.extuni.enum_demo4 
    -DartifactId=enum_demo4 
    -DarchetypeArtifactId=maven-archetype-quickstart 
    -DinteractiveMode=false 
    -DarchetypeVersion=1.4
Maven Directory Layout
Continue reading