0

So I'm writing a Java program that calculates the difference between the sum of the squares of the first 100 integers and the square of the sum of the first 100 integers:

class project_euler6 { public static int sum_of_squares(int start, int end) { int total = 0; while (start <= end) { total += (start * start); start++; } return total; } public static int square_of_sums(int start, int end) { int total = 0; while (start <= end) { total += start; start++; } total *= total; return total; } public static void main(String[] args) { int first_total = sum_of_squares(1, 100); int second_total = square_of_sums(1, 100); int difference = Math.abs(first_total - second_total); System.out.println("The difference between the sum of the squares and the square of the sums of first 100 integers is " + difference); } } 

When I run it, I get this strange error:

Exception in thread "main" java.lang.NoClassDefFoundError: project_euler6/java Caused by: java.lang.ClassNotFoundException: project_euler6.java at java.net.URLClassLoader$1.run(URLClassLoader.java:202) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:306) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:247) 

Not sure where to start on this one...

Any help much appreciated, Mariogs

4
  • Unfortunately, "NoClassDefFoundError" is Java's garbage exception -- about a dozen different errors rolled into one. But in most cases the problem has something to do with mismatched jar files. Commented May 30, 2014 at 3:14
  • 7
    In this case, though, it appears to have something to do with this: project_euler6.java -- you're probably running java project_euler6.java rather than java project_euler6. Commented May 30, 2014 at 3:16
  • Edit your question and add the full command line you are using to start the app... Commented May 30, 2014 at 3:19
  • 1
    Can you please show how you call your program Commented May 30, 2014 at 5:45

3 Answers 3

2

The problem is not with the code nor the access specifier with the class (public is not required before the class name). The actual problem is how are you executing it. I assume that you might have used java project_euler6.java instead of java project_euler6.

Sign up to request clarification or add additional context in comments.

Comments

1

For java, At least one public class is required in main file. So, you have to make the class project_euler6 as public.

public class project_euler6 { /*** your code ***/ } 

1 Comment

I think that might depend on the JVM. I would have thought that main must be in a public class, but I just tried it and was able to execute a main method out of a non-public class. Using Mac OSX & 1.7.0_25.
0

Try to use editor like eclipse, simply create new project and copy-paste the above code. Code looks correct and should compile and give result.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.