在mac终端上运行Java类 - 错误:无法找到或加载主类类型

时间:2016-12-28 22:37:56

标签: java macos

我对Java很新;并且只有自学了基本的java类。现在我正在研究遗产部分;这是我从网上获得的示例代码。

我的代码在我的sublime上,然后我编译并运行在我的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现在都保存在我的工作区文件夹中;这也是我的工作目录

有人可以更详细地解释这一点(不仅仅是解决方案,但为什么会发生这种情况以及如何解决)

1 个答案:

答案 0 :(得分:2)

您在包Types.java中定义了types,您需要将您的java文件放在文件夹types中。

然后使用

javac -classpath . types/Types.java

java -classpath . types.Types

您可以使用类路径here

进行类似的讨论

您也可以参考java docs了解类路径