Exécution de la classe Java sur un terminal mac-Erreur: Impossible de trouver ou de charger les types de classe principaux


Je suis très nouveau en Java; et j'ai seulement appris la classe java de base. Maintenant, j'étudie la partie héritage; voici l'exemple de code que j'ai obtenu en ligne.

J'ai mon code sur mon sublime, puis je compile et exécute dans mon terminal mac.

Je crois que l'erreur se produit lorsque je charge mon paquet

Voici mon code:

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]);
    }
}

Voici ce qui se passe quand je l'exécute sur mon 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$ 

Mes deux types.de classe et les Types.java sont maintenant enregistrés dans mon dossier workspace; et c'est aussi mon répertoire de travail

Quelqu'un peut-il expliquer cela plus en détail (pas seulement la solution, mais pourquoi cela se produit-il et comment y remédier)

Author: Jonah, 2016-12-29

1 answers

Vous avez défini Types.java package types, vous devez placer votre fichier java dans le dossier types.

, Puis utilisez

javac -classpath . types/Types.java

java -classpath . types.Types

Vous pouvez obtenir similaire discussion sur l'utilisation du chemin de classe ici

Vous pouvez également vous référer à java docs pour comprendre classpath

 2
Author: ravthiru, 2017-05-23 11:45:58