Eclipse java.lang.NoClassDefFoundError

时间:2017-09-23 06:33:46

标签: java eclipse

我在http://algs4.cs.princeton.edu/14analysis/Stopwatch.java.html尝试了秒表课程。我正在使用Eclipse,这是我做过的事情 -

这是代码 -

public class HelloWorld {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int n = Integer.parseInt(args[0]);

        // sum of square roots of integers from 1 to n using Math.sqrt(x).
        Stopwatch timer1 = new Stopwatch();
        double sum1 = 0.0;
        for (int i = 1; i <= n; i++) {
            sum1 += Math.sqrt(i);
        }
        double time1 = timer1.elapsedTime();
        StdOut.printf("%e (%.2f seconds)\n", sum1, time1);

        // sum of square roots of integers from 1 to n using Math.pow(x, 0.5).
        Stopwatch timer2 = new Stopwatch();
        double sum2 = 0.0;
        for (int i = 1; i <= n; i++) {
            sum2 += Math.pow(i, 0.5);
        }
        double time2 = timer2.elapsedTime();
        StdOut.printf("%e (%.2f seconds)\n", sum2, time2);
    }

}

我已将外部JAR stdlib添加到Java构建路径

然而,当我运行它时,我仍然得到错误 -

Error message

有人可以帮帮我,告诉我我做错了什么吗?

2 个答案:

答案 0 :(得分:0)

问题出在StdOut行。 JDK中不存在该类。通常它用于教学目的,如here

所以,要解决你的类未找到异常,请将其替换为:

public class HelloWorld {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int n = Integer.parseInt(args[0]);

        // sum of square roots of integers from 1 to n using Math.sqrt(x).
        Stopwatch timer1 = new Stopwatch();
        double sum1 = 0.0;
        for (int i = 1; i <= n; i++) {
            sum1 += Math.sqrt(i);
        }
        double time1 = timer1.elapsedTime();
        System.out.println(String.format("%e (%.2f seconds)\n", sum1, time1));

        // sum of square roots of integers from 1 to n using Math.pow(x, 0.5).
        Stopwatch timer2 = new Stopwatch();
        double sum2 = 0.0;
        for (int i = 1; i <= n; i++) {
            sum2 += Math.pow(i, 0.5);
        }
        double time2 = timer2.elapsedTime();
        System.out.println(String.format("%e (%.2f seconds)\n", sum2, time2));
    }
}

您可能已经注意到,现在有两个System.out.println

答案 1 :(得分:0)

当我按照此处列出的步骤进行操作时How to import a jar in Eclipse

谢谢大家的时间!