-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCmd.java
More file actions
39 lines (31 loc) · 1.39 KB
/
Cmd.java
File metadata and controls
39 lines (31 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package src;
import java.util.Scanner;
public class Cmd {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("클래스 이름을 입력하세요 (예: Pwd, ManPwd, Ls, Cd, Cat, exit): ");
String className = scanner.nextLine();
if ("exit".equalsIgnoreCase(className)) {
System.out.println("프로그램을 종료합니다.");
break;
}
try {
// 첫 글자를 대문자로 변환하여 클래스 이름 일치시키기
String formattedClassName = className.substring(0, 1).toUpperCase() + className.substring(1);
// src 패키지의 클래스 로드
Class<?> clazz = Class.forName("src." + formattedClassName);
// main 메서드 호출
clazz.getMethod("main", String[].class)
.invoke(null, (Object) new String[]{});
} catch (ClassNotFoundException e) {
System.out.println("클래스를 찾을 수 없습니다: " + className);
} catch (NoSuchMethodException e) {
System.out.println("해당 클래스에 main 메서드가 없습니다.");
} catch (Exception e) {
e.printStackTrace();
}
}
scanner.close();
}
}