简体   繁体   中英

Could not find or load main class in java due to placing a package name

Before i create a package name to my class i done my work correctly. i faced problem like ** Could not find or load main class** after giving a package name to my class will you please check my below code

package c2.get.pack;
import java.util.*;
import java.lang.*;
class FindDiff
{
    public static void main(String[] args) 
    {
        ArrayList<Integer> arr=new ArrayList<Integer>();
        arr.add(1);
        arr.add(2);
        arr.add(5);
        arr.add(5);
        arr.add(7);
        arr.add(7);
        arr.add(11);
        System.out.println("Prime number in ArrayList :"+arr);
        Set<Integer> set=new HashSet<Integer>(arr);
        arr.clear();
        arr.addAll(set);
        System.out.println("Prime number in Set :"+arr);
        Iterator itr=set.iterator();
        while(itr.hasNext()){
        int num=(int)itr.next();
        System.out.println(num);
    }
}

i got an error result like this
first i compile the file with class name like

G:\\java\\java_programs\\logics>javac FindDiff.java

after that had try to run the program with package name i give an error

G:\\java\\java_programs\\logics>java c2.get.pack.FindDiff Error: Could not find or load main class c2.get.pack.FindDiff

What the error here from my side.Please note that i used notepad to write a program.After compiling the program it's not create any package

ok. i copied your code and figured it out. first compile everything with

javac *.java -d .

then the package, declared in FindDiff, will be created as folder structure and the .java files will be compiled into these

no you can run it by writing

java c2.get.pack.FindDiff

The FindDiff class is not public, therefore the main method you have declared within cannot be taken as an entry point.

Make FindDiff public.

public class FindDiff { /* ... */ }

[Edit: further discussion and discovery of the problems in comments led to Tom K's answer, which solves the filename problems]

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