-2

I'm using NetBeans to write the program. The program calculates the area of a circle

The code :

public class Pragnya {

    public static void main(String[] args) {
        int r = Integer.parseInt(args[0]);
        double PI = 3.14;
        double area = PI * r * r;
        System.out.println("Area of circle is: "+area);
    }
    
}

The error shown is :

Error: Could not find or load main class Pragnya
Caused by: java.lang.NoClassDefFoundError: pragnya/Pragnya (wrong name: Pragnya)

How do I resolve this?

khelwood
  • 55,782
  • 14
  • 81
  • 108
Pragnya
  • 1
  • 1

1 Answers1

0

If you're running this within netbeans, this error can't/shouldn't occur (then you did quite a number on how you managed to misconfigure this project!). If you're running it from the command line:

cd ..
java pragnya.Pragnya

Alternatively, assuming JDK11+, you can just run java Pragnya.java (not javac, then java - just java the source file).

Read up on packages and classpath structure if you want to know why you need to it this way.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72
  • Thank you Now it shows Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0 at pragnya.Pragnya.main(Pragnya.java:15) i.e after compiling using java Pragnya.java because i have version 15 of JDK – Pragnya Jan 26 '21 at 13:57
  • `int r = Integer.parseInt(args[0]);` because you don have parsed an command line argument? – Aalexander Jan 26 '21 at 14:00
  • Thanks but in cmd i pass an argument i.e java Pragnya 3 and it shows that error – Pragnya Jan 26 '21 at 14:02
  • No, it won't. You're passing it wrong, then. – rzwitserloot Jan 26 '21 at 16:11