-2

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

sreeku24
  • 320
  • 3
  • 5
  • 16

2 Answers2

3

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
Tom K
  • 320
  • 2
  • 9
  • ""javac *.java -d.FindDiff.java"" i try to compile like it's not gives the result. Will you please tell me exactly – sreeku24 Jan 09 '17 at 19:09
  • @sreeku24 Just write it as i did in the post, dont change the command! – Tom K Jan 09 '17 at 19:10
  • G:\java\java_programs\logics>javac *.java -d . G:\java\java_programs\logics>java c2.get.pack.c1 Error: Could not find or load main class c2.get.pack.c1 – sreeku24 Jan 09 '17 at 19:12
  • @sreeku are you sure that error came when you typed that command? – Tom K Jan 09 '17 at 19:15
  • it's create the folders automatically in my root directory but i will try to run the second command what you mentioned there it's not working – sreeku24 Jan 09 '17 at 19:17
  • sorry Tom it's working but instead of using this"G:\java\java_programs\logics>java c2.get.pack.c1" to "G:\java\java_programs\logics>java c2.get.pack.FindDiff" – sreeku24 Jan 09 '17 at 19:19
  • 1
    Thanks for Helping @TomK – sreeku24 Jan 09 '17 at 19:20
  • @sreeku24 sorry, i renamed the java file to c1 on my computer, the correct command is "java c2.get.pack.FindDiff" – Tom K Jan 09 '17 at 19:20
  • Will you please change your answer "FindDiff" instead of "c1" – sreeku24 Jan 09 '17 at 19:21
-1

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]

Brandon McKenzie
  • 1,655
  • 11
  • 26
  • 1
    @sreeku24 Making it public (which is necessary) doesn't mean you don't need to put it in the correct directory. – Dave Newton Jan 09 '17 at 18:40
  • You don't have to have a public class to run `main`. In fact, you can have *several* non-public classes in the same file, each with their own `main`. – Makoto Jan 09 '17 at 18:41
  • @DaveNewton, yes i make my class FindDiff as public and again it's gives same error like "Could not find or load main class c2.get.pack.FindDiff" – sreeku24 Jan 09 '17 at 18:43
  • @sreeku24 I'm not sure how much plainer I can make it. You're compiling a Java file that's in your current directory. That directory does not match the package name contained in said Java file. The Java source must be located in a directory hierarchy that matches the package hierarchy. E.g., http://stackoverflow.com/q/9510932/438992 etc. – Dave Newton Jan 09 '17 at 18:44
  • @DaveNewton, i saved my file in G:\java\java_programs\logics – sreeku24 Jan 09 '17 at 18:47
  • @sreeku24 Yes, I know; that's what I said. What I'm saying is that won't work, because it needs to be in `../c2/get/pack/FindDiff.java`, because Java. – Dave Newton Jan 09 '17 at 18:48
  • @DaveNewton, program not generate folders directly – sreeku24 Jan 09 '17 at 18:50
  • @sreeku24 ... Neat. https://docs.oracle.com/javase/tutorial/java/package/managingfiles.html – Dave Newton Jan 09 '17 at 18:50
  • @DaveNewton, yes i understand what your saying. After creating the folders manually i got the final result – sreeku24 Jan 09 '17 at 18:53