简体   繁体   中英

Java Maven Build: Thread.sleep doesn't work (in eclipse it does)

I've got a strange error. My code is the following:

    LoginQueue queue = new LoginQueue(server);
    QueueTimer timer = queue.waitInQueue(this.username, this.password);
    Thread.currentThread().sleep(6000);
    while (!timer.isFinished()) {

        if (timer.getPosition() != 0L) {
            log("Queue Position" + server.toString() + ":"
                    + String.valueOf(timer.getPosition()), LogLevel.DEBUG);
            Thread.currentThread().sleep(3000);
        }

My goal is to login to the League of Legends servers using the open source riotapi: https://github.com/loldevs/riotapi/

The problem isn't LoL specific, so no problem there. When I create the QueueTimer , it needs some time to retrieve some data from the server, otherwise timer.getPosition() will throw a NullPointerException . That's why I wait 6 seconds before going on. In Eclipse debugging it all works fine, but after exporting it with m2eclipse (clean package as goals; I'm new to Maven if that matters), the compiled JAR doesn't sleep. It just goes on and then throws my NullPointerException because it didn't wait. Why does my code behave in Eclipse and doesn't work outside? What am I doing wrong? I've tried Thread.sleep() instead of Thread.currentThread().sleep() , doesn't work either.

Edit:

I changed it to:

LoginQueue queue = new LoginQueue(server);
String authkey = queue.waitInQueueBlocking(this.username, this.password);

So I am using synchronous methods now. Now I get the following stacktrace:

Exception in thread "Queue Timer for LeagueAlerter" java.lang.NullPointerException
        at org.apache.cxf.jaxrs.client.AbstractClient.setupOutInterceptorChain(AbstractClient.java:840)
        at org.apache.cxf.jaxrs.client.AbstractClient.createMessage(AbstractClient.java:902)
        at org.apache.cxf.jaxrs.client.WebClient.finalizeMessage(WebClient.java:1068)
        at org.apache.cxf.jaxrs.client.WebClient.doChainedInvocation(WebClient.java:1049)
        at org.apache.cxf.jaxrs.client.WebClient.doInvoke(WebClient.java:854)
        at org.apache.cxf.jaxrs.client.WebClient.doInvoke(WebClient.java:825)
        at org.apache.cxf.jaxrs.client.WebClient.invoke(WebClient.java:393)
        at org.apache.cxf.jaxrs.client.WebClient$SyncInvokerImpl.method(WebClient.java:1582)
        at org.apache.cxf.jaxrs.client.WebClient$SyncInvokerImpl.method(WebClient.java:1577)
        at org.apache.cxf.jaxrs.client.WebClient$SyncInvokerImpl.post(WebClient.java:1517)
        at org.apache.cxf.jaxrs.client.spec.InvocationBuilderImpl.post(InvocationBuilderImpl.java:141)
        at net.boreeas.riotapi.loginqueue.LoginQueue.getAuthToken(LoginQueue.java:54)
        at net.boreeas.riotapi.loginqueue.QueueTimer.run(QueueTimer.java:89)

The error seems to be somewhere in the Riot API. But why does it work inside eclipse? I am too stupid to understand the API source, would be so cool if someone helped (Github is https://github.com/loldevs/riotapi/ ). My pom.xml: http://pastebin.com/jik5ba8J

Hey fellow ${search engine of choice}ers!

I had the same problem when running the .jar of my application which was built by maven.

Turns out that

  1. I was using the maven assembly plugin to generate the jar. This plugin will (at least by default) put all the dependency resources directly in the produced jar file
  2. CXF has some overlapping resources (which means trouble because of pt. 1)

I replaced the assembly plugin with the one-jar plugin (which adds dependencies as jar files in the produced binary) and after this everything worked fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM