简体   繁体   中英

Trouble in compiling the java code

I wrote the following piece of code in sublime text on my macbook pro. I saved the file inside the java folder on my desktop. When I tried to compile the program and tried to execute it, I am getting the fallowing error message " Error: Could not find or load main class Animals ". Could someone help me in compiling and running this program

package forest;
class Animals{

    public static void main(String[] args)
    {
        Animals s = new Animals();
        Sring[] s2 = s.getAllAnimals();

    }

    public String[] getAllAnimals()
    {
        String[] s1= {"Lion", "Elephant", "Tiger","Deer","Wolf","Dinosar"};
        return s1;
    }
}

将您的类Animal设为public,并将该文件重命名为Animal.java

  1. Your class is in a package called forest so you need to move Animals.java into a directory called forest .

  2. You hava a typo on line 7: Sring[] s2 = should be String[] s2 = .

  3. Compile from the parent directory of forest . Something like this should work

     $ javac forest/Animals.java 
  4. Run from the parent dir:

     $ java forest.Aminals 

Your program will have no output when you run it.

Look on your code there is no sring type class in java so change it to String

 Sring[] s2 = s.getAllAnimals();
    change it to 
     String[] s2 = s.getAllAnimals();

Everything will be good

First of all you can name your java file with any name. Say Sample.java. and let us consider this file is present in D:\\work

Go to D:\\work folder in cmd prompt.

Keep this in mind :

If you write package forest, then your class has to be public. So write public class Animals{}

While compiling do this javac -d Sample.java By doing this the compiler will create the folder "forest" and inside that it will create the Animals.class. So now in D:\\work we have Sample.java file and "forest" folder

Now in your command prompt DO NOT go inside the "forest" folder. Stay in work folder. D:\\work

And Run this command from D:\\work java forest.Animals

Explantion::

The name of your class that contains Main method is not Animals. It is forest.Animals This is the fully qualified name of the class. This happened becasue you put a package to it. So while runnning you must run with fully qualified name

 java forest.Animals

The name of your class is not Sample.java. <-- This is just a text file name it anything the complier will not generate a class with name Sample.class because inside this file there is no such class, the class name is Animals. So Animals.class will be generated.

And javac -d Sample.java helps you create the folder structure automatically based on the package.

For example if your class was like this

package com.stackoverflow.samples
public class Animals{
}

And you do java -d Sample.java

The Compiler will create a folder com inside that another folder stackoverflow inside that another folder samples and inside that a file Animals.class

To run this you must run it from outside the the "com" folder with fully qualified name

java com.stackoverflow.samples.Animals

尝试本教程Java和Mac OS X终端public您的类,并确保包含源代码的文件名和main class的名称相同,您的主类是包含main方法的类。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM