Mac 터미널에서 Java 클래스 실행 중-오류 : 기본 클래스 유형을 찾거나로드 할 수 없습니다.

ElleryL

저는 Java를 처음 접했습니다. 기본 Java 클래스를 스스로 배웠습니다. 이제 상속 부분을 공부하고 있습니다. 다음은 온라인에서 얻은 샘플 코드입니다.

내 코드가 숭고한 상태에서 Mac 터미널에서 컴파일하고 실행합니다.

패키지를로드 할 때 오류가 발생한다고 생각합니다.

내 코드는 다음과 같습니다.

package types;

public class Types {

    public static void main(String[] args) {

        // == Types ==
        // Every variable has a type. Types fall into two categories.
        // (1) Primitive types
        // - Primitive values are NOT objects.
        // - The value (not a reference to it) sits directly in the box.
        // - byte, short, int, long, float, double, boolean, char
        // - Primitive type names begin with lowercase letters.

        // Declare that age is a variable and its type is int; assign it
        // the value 5.
        // The variable does not contain a reference to an object with value 5;
        // the variable contains 5 itself.
        int age = 5;
        boolean lovesPrincesses = true;
        double shoeSize = 12.5;
        char initial = 'C';

        // We can't redeclare a variable.
        // int age = 8;

        // But we can assign a new value to it.
        age = 8;

        // We can't assign it anything other than int values.
        // age = 8.7;
        // age = true;

        // (2) Class types
        // - the variable contains a reference to an object.
        // - We must explicitly construct each object using "new"

        // Declare that s1 is a variable whose type is String; construct a new
        // object of type String and store a reference to it in s1.
        String s1 = new String("hello");

        // == Wrapper classes and autoboxing ==

        // Every primitive type has a wrapper class version. It can be used to
        // represent a primitive value when an Object is needed.
        Integer i2 = new Integer(5);
        Boolean b2 = new Boolean(false);
        System.out.println(i2);
        System.out.println(b2);

        // Java can automatically "box" a primitive into an instance of its
        // wrapper class.
        Integer x = 6; // automatically does Integer i = new Integer(6)

        // And it can automatically "unbox" a wrapper object to get the
        // primitive value.
        int y = x + 4; // automatically does int y = x.intValue() + 4;

        // == Strings ==
        // Strings are objects.
        // Strings are immutable.

        // You can construct a String explicitly, but Java allows a shortcut:
        // you can omit the "new".
        String s = new String("hello");
        String s2 = "bye";

        // Because Strings are immutable, this actually constructs a brand new
        // String rather than appending to the old one.
        s2 = s + s2;
        System.out.println(s2);

        // Indexing
        char letter = s2.charAt(3); // Python: s2[3]
        System.out.println(letter);

        // Slicing
        String slice = s2.substring(4); // Python: s2[4:]
        System.out.println(slice);
        slice = s2.substring(5, 7); // Python: s2[5:7]
        System.out.println(slice);

        // Stripping (remove whitespace from beginning and end of String.)
        String s3 = "    hi   there   ";
        s3 = s3.trim();
        System.out.println(s3);

        // Splitting.
        s3 = "before    hi   there   after";
        String[] parts = s3.split("e");
        System.out.println(parts[0]);
        System.out.println(parts[1]);
        System.out.println(parts[2]);
        // Out of bounds:
        // System.out.println(parts[3]);

        // == Arrays ==
        // Not like Python lists!:
        // - fixed length, which is set when constructing the object
        // - all elements must have the same type

        int[] intArray = new int[4];
        System.out.println(intArray[0]);

        String[] stringArray = new String[20];
        System.out.println(stringArray[0]);
        stringArray[0] = "blunderbuss";
        System.out.println(stringArray[0]);

        intArray = new int[] { 1, 2, 3 };
        System.out.println(intArray[1]);
    }
}

내 temrinal에서 실행하면 어떻게 되나요?

Last login: Wed Dec 28 05:38:20 on ttys000
LaitekiMacBook-Pro:~ Lai$ cd Documents
LaitekiMacBook-Pro:Documents Lai$ cd workspace
LaitekiMacBook-Pro:workspace Lai$ ls
Types.java      sample_test.java
LaitekiMacBook-Pro:workspace Lai$ javac Types.java
LaitekiMacBook-Pro:workspace Lai$ java Types
Error: Could not find or load main class Types
LaitekiMacBook-Pro:workspace Lai$ java types.Types
Error: Could not find or load main class types.Types
LaitekiMacBook-Pro:workspace Lai$ 

이제 Types.class와 Types.java가 모두 내 작업 공간 폴더에 저장됩니다. 그리고 이것은 내 작업 디렉토리이기도합니다.

누군가이 이것을 더 자세히 설명 할 수 있습니까?

ravthiru

Types.java패키지 types에서 정의한 경우 Java 파일을 폴더에 배치해야합니다 types.

그런 다음

javac -classpath . types/Types.java

java -classpath . types.Types

여기에서 클래스 경로 사용에 대한 유사한 토론을 얻을 수 있습니다.

Java 문서참조 하여 클래스 경로를 이해할 수도 있습니다.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관