자바 메인함수에서 args 질문드립니다.

conan1447의 이미지

public class Practice {
    public static void main(String[] args) throws Exception {
        // Check command line parameter usage
        if (args.length != 2) {
            System.out.println("Usage: java Practice John practice.txt");
            System.exit(1);
        }
 
        // Check if file exists
        File file = new File("practice.txt");
        if (!file.exists()) {
            System.out.println("File " + args[1] + " does not exist");
            System.exit(2);
        }
    }
}

인텔리제이로 위의 자바 코드를 작성했는데,

첨부한 그림대로,
edit configuration에서 Program arguments에 2개의 arg를 추가했는데도
if문이 실행되어 결과가 다음 같이 나오네요.

Usage: java Practice John practice.txt

Process finished with exit code 0

if문이 실행되지 않게 하려면 어떻게 해야 하나요?

File attachments: 
첨부파일 크기
Image icon 제목 없음.png99.11 KB
익명 사용자의 이미지

이런 문제에 부딛쳤을 때는...

args.length 값이 무엇인지, args에는 어떤 값이 들어왔는지 출력하게 해 보면 문제 해결에 스스로 더 가까이 갈 수 있겠지요.

swish95의 이미지

실제 실행되는 인자를 모르는데 답이 나올리가 있나요

------------------------------------------------------------
ProgrammingHolic