Code

JAVA 프로젝트 시작 방법, VS Code 이용 빌드 디버깅

codens 2024. 11. 11. 07:23

JAVA 프로젝트 시작 방법

* JDK 설치
https://www.oracle.com/kr/java/technologies/downloads

https://aws.amazon.com/corretto

설치후 "where java" 명령으로 경로 설정 확인
설치된 곳이 "C:\Program Files\Java\jdk-21" 라면
"C:\Program Files\Java\jdk-21\bin" 폴더를 환경변수 PATH 에 경로 설정
setx JAVA_HOME "C:\Program Files\Java\jdk-21"
setx PATH "%PATH%;%JAVA_HOME%\bin"


* Maven 설치
https://maven.apache.org/
프로젝트 관리 : 빌드, 실행, 테스트
압축풀고 환경변수  PATH 에 경로 설정

 

//-------------------------------------

* vscode extension 설치

Extension Pack for Java



//-----------------------------------------------------------------------------
4개 파일 생성
//-------------------------------------
// src/main/java/com/example/App.java - 메인 클래스
package com.example;
public class App {
    public static void main(String[] args) {
        System.out.println("hello world");
    }
}

//-------------------------------------
// .vscode/launch.json
{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "java",
            "name": "Debug App",
            "request": "launch",
            "mainClass": "cohttp://m.example.App",
            "projectName": "ex1"
        }
    ]
}
//-------------------------------------
// .vscode/settings.json
{
    "java.project.sourcePaths": [ "src/main/java" ],
    "java.project.outputPath": "bin",
    "java.project.referencedLibraries": [
        "lib/**/*.jar"
    ]
}


//-------------------------------------
// pom.xml  - Maven 프로젝트 파일

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>프로젝트이름</artifactId>
    <version>1.0</version>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.3.0</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.example.App</mainClass>
                            <addClasspath>true</addClasspath>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.5.0</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>

                        <configuration>
                            <transformers>
                                <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.example.App</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>

                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

</project>




//-----------------------------------------------------------------------------

* 실행 방법1 : 
vscode 에서 Run -> Start Debugging (F5) 선택

//-------------------------------------
* 실행 방법2 : java 사용
# 컴파일
javac -d bin src/main/java/com/example/*.java src/main/java/com/example/util/*.java

# 실행
java -cp bin cohttp://m.example.App

//-------------------------------------
실행 방법3 :  Maven 사용

# 컴파일
mvn compile

# 실행
mvn exec:java -Dexec.mainClass="cohttp://m.example.App"

# 테스트
mvn test

# 실행용 압축파일 JAR 생성 (컴파일도 실행)
mvn package

실행
java -jar target/myapp.jar

 

 

반응형