3

When compiling the program I get the error as

Could not find the main class: Solution. Program will exit.

The program is:

import java.util.*;

public class Solution{
   public static long[] factors(long a){
     long[] b;
     b=new long[50];
     int count=0;
     for(long i=1L;i<=a/2;i++)
       if(a%i==0) b[count++]=i;
     return b;
   }

   public static void main(String[] args) {

        Scanner in=new Scanner(System.in);

        int N=in.nextInt();
        long K=in.nextInt();
        long[] fact=factors(K);
        l1:
        for(int i=0;i<N;i++)
        {
            long num=in.nextInt();
            for(int j=0;j<fact.length;j++)
                if(num%fact[j]==0 && fact[j]!=1) {fact[j]=1;continue l1;}

        }
        int result=0;
        for(int i=0;i<fact.length;i++)
            if(fact[i]!=1) ++result;
        System.out.println(result);
    }
}
Roman C
  • 49,761
  • 33
  • 66
  • 176
harvish
  • 195
  • 1
  • 4
  • 11
  • Show us how you're running the program. – Mark Elliot Sep 30 '12 at 15:44
  • what do you use for compiling this code? An IDE like Eclipse or Netbeans or with any other tool? – Jan Koester Sep 30 '12 at 15:47
  • @LewsTherin valid Java syntax, it's what's known as a "label" (note the `continue l1;` statement that appears later. – Mark Elliot Sep 30 '12 at 15:53
  • i m running this thro terminal... javac Solution.java java Solution – harvish Sep 30 '12 at 15:54
  • Look at the last answer. – Lews Therin Sep 30 '12 at 16:04
  • @JanKoester can yu tell me what difference it makes in compiling in different compilers??? i compiled thro terminal which showed me the error said above but when i compiled thro Intellij Idea it got compiled without any error.. – harvish Sep 30 '12 at 16:36
  • @harvish problems with dependencies can occur. In an IDE like IntelliJ all needed libraries for your program are added automatically to your projects class path. If you compile it direct in a console it can happen that some libraries could not be found because you have not added them to the class path of your project. – Jan Koester Sep 30 '12 at 17:41

4 Answers4

1

This will not compile because the main method is not belong to a class. Put the main method inside the class to solve the problem. And your code is throwing arithmetic exception divide by zero should be fixed like that.

for(int j=0;j<fact.length;j++)
  if (fact[j] != 0)
    if(num%fact[j]==0 && fact[j]!=1) {
      fact[j]=1;continue l1;
    }
Roman C
  • 49,761
  • 33
  • 66
  • 176
1

Your code is correct - it compiles and runs fine on ideone (link).

The problem that you are seeing has to do with the way you are compiling and running your application. At the command line prompt, do this:

javac Solution.java

This will produce Solution.class file. Run it as follows:

java Solution

At this point the running program will be reading the input and producing the output on the console. It will throw exceptions if the expected input is not available because you call nextInt without checking for hasInt, but it will produce a result if you give it the input that it expects.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • @It will never produce result until errors in the code is fixed. – Roman C Sep 30 '12 at 16:12
  • @RomanC The same code is getting compiled in intellij idea but not compiling thro terminal. – harvish Sep 30 '12 at 16:24
  • @dasblinkenlight Can yu explain me what to do to correct the error?...put `hasInt` ?? – harvish Sep 30 '12 at 16:27
  • @harvish Actually I've already forgot when I did it manually via console. If I need the automation of the building process I prefer the Ant or Maven. And the above is a good support for plug-ins of the IDE. – Roman C Oct 10 '12 at 09:58
0

When compiling the program i get the error as

Could not find the main class: Solution. Program will exit.

Compiling doesn't require any main class: you can compile helper classes independently. So the problem is apparently that you're trying to run a class that you haven't compiled yet. For example, if you're using the command-line tools, then you're most likely running java when you mean to be running javac.

Community
  • 1
  • 1
ruakh
  • 175,680
  • 26
  • 273
  • 307
0

There are NO independent functions in the Java programming model; every procedure/function must be a method on some class, including the static void main method.

Richard Sitze
  • 8,262
  • 3
  • 36
  • 48