NoSuchMethodError:tools.Timer。(J)V

时间:2022-02-26 01:28:23

I have a Timer class and a Test class to test this timer:

我有一个定时器类和一个测试类来测试这个计时器:

package tools;

public class Timer extends Thread 
{
    public boolean isRunning = true;
    private long timeout = 0;

    public Timer(long aTimeout)
    {
        timeout = aTimeout;
    }

    // Run the Thread
    public void run()
    {
        int i = 1000;
        while(i <= timeout)
        {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            i = i + 1000;
        }
        isRunning = false;
    }
}

And the Test Class:

测试类:

public class Test 
{
    public static void main(String[] args) 
    {
        Timer myTimer = new Timer(10000);
        myTimer.start();

        while(myTimer.isRunning)
        {
            System.out.println("Running");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

In Eclipse this works well. When I include it to another project on a Solaris Server, I get the following exception:

在Eclipse中,这很好用。当我将它包含到Solaris服务器上的另一个项目时,我得到了以下的异常:

Exception in thread "main" java.lang.NoSuchMethodError: tools.Timer.<init>(J)V 

I googled it but I could not find any answer - why is this not working ? Cheers, Tim.

我用谷歌搜索了一下,但我找不到任何答案——为什么这行不通?干杯,蒂姆。

2 个解决方案

#1


1  

You're constructing timer like this:

你正在构造这样的定时器:

Timer myTimer = new Timer();

And your constructor declaration is:

您的构造函数声明是:

public Timer(long aTimeout)

Obvious, isn't it? You must either construct the timer like new Timer(1234), or add a parameterless constructor to it.

很明显,不是吗?您要么构造计时器,比如新计时器(1234),要么添加一个无参数的构造函数。

#2


0  

The code as you show it shouldn't even compile, as you are calling the Timer() default constructor, however Timer only has a parametrized constructor: public Timer(long aTimeout).

当您调用Timer()默认构造函数时,您所显示的代码甚至不应该编译,但是Timer只有一个参数化构造函数:public Timer(long aTimeout)。

So either you aren't showing us an SSCCE, or your definition of "works well" is significantly different from ours ;-)

因此,要么你没有向我们展示SSCCE,要么你对“works well”的定义与我们的截然不同;

#1


1  

You're constructing timer like this:

你正在构造这样的定时器:

Timer myTimer = new Timer();

And your constructor declaration is:

您的构造函数声明是:

public Timer(long aTimeout)

Obvious, isn't it? You must either construct the timer like new Timer(1234), or add a parameterless constructor to it.

很明显,不是吗?您要么构造计时器,比如新计时器(1234),要么添加一个无参数的构造函数。

#2


0  

The code as you show it shouldn't even compile, as you are calling the Timer() default constructor, however Timer only has a parametrized constructor: public Timer(long aTimeout).

当您调用Timer()默认构造函数时,您所显示的代码甚至不应该编译,但是Timer只有一个参数化构造函数:public Timer(long aTimeout)。

So either you aren't showing us an SSCCE, or your definition of "works well" is significantly different from ours ;-)

因此,要么你没有向我们展示SSCCE,要么你对“works well”的定义与我们的截然不同;