Search code examples
javajvmjvm-hotspotorg.json

new org.json.JSONObject(String) is blocking indefinitely when run from jar file only


I have a SwingWorker background thread that contains the following code to perform a request and parse the response:

(new SwingWorker<Void, Void>() {
    @Override
    protected Void doInBackground() {
...
    HttpRequest listRequest = HttpRequest
        .newBuilder(url)
        .GET()
        .build();

    HttpResponse<String> listResponse = client
        .send(listRequest, HttpResponse.BodyHandlers.ofString());

    /* listResponse.body() is correct and can be printed/logged */

    JSONObject listing = new JSONObject(listResponse.body()); /* hangs when run from JAR */
...
    return null;
  }
}).execute();

The version of org.json is 20190722 (I've also tried 20800813) and the JVM is Oracle HotSpot 12.0.1 2019-04-16 (build 12.0.1+12)

The maven compiler source and target are set to 9 in the POM. (And Java-SE9 in eclipse.)

When I run the code from eclipse it launches it directly from the target classes using the command:

/Library/Java/JavaVirtualMachines/jdk-12.0.1.jdk/Contents/Home/bin/java \
  -Dfile.encoding=UTF-8 \
  -classpath \
    /Users/example/workspace/upload-client/target/classes:\
    /Users/example/.m2/repository/org/projectlombok/lombok/1.18.10/lombok-1.18.10.jar:\
    /Users/example/.m2/repository/org/json/json/20190722/json-20190722.jar \
  com.example.UploadClient

and everything is fine, but when I load it from the JAR using this slightly modified command:

/Library/Java/JavaVirtualMachines/jdk-12.0.1.jdk/Contents/Home/bin/java \
  -Dfile.encoding=UTF-8 \
  -jar ./target/upload-client-1.0.jar \
  com.example.UploadClient

it hangs on the line JSONObject listing = new JSONObject(listResponse.body());. I've inserted logging around the line to determine that the listResponse.body() returns a 5k string of correct json just as expected.

When run from the jar, execution stops at this line and does not resume. Jstack output is listed here:

2019-10-22 13:37:55
Full thread dump Java HotSpot(TM) 64-Bit Server VM (12.0.1+12 mixed mode, sharing):

Threads class SMR info:
_java_thread_list=0x00007f9ac10242d0, length=18, elements={
0x00007f9ac0819000, 0x00007f9ac081c000, 0x00007f9ac0805800, 0x00007f9ac1803000,
0x00007f9ac1804800, 0x00007f9ac1808000, 0x00007f9ac1821000, 0x00007f9ac088f000,
0x00007f9ac010a000, 0x00007f9ac010c000, 0x00007f9abfa00800, 0x00007f9ac2011800,
0x00007f9ac0187000, 0x00007f9ac01b1000, 0x00007f9abfc2f000, 0x00007f9ac02f3800,
0x00007f9ac00da800, 0x00007f9ac19ce000
}

"Reference Handler" #2 daemon prio=10 os_prio=31 cpu=0.62ms elapsed=127.37s tid=0x00007f9ac0819000 nid=0x3a03 waiting on condition  [0x000070000d2d1000]
   java.lang.Thread.State: RUNNABLE
    at java.lang.ref.Reference.waitForReferencePendingList([email protected]/Native Method)
    at java.lang.ref.Reference.processPendingReferences([email protected]/Reference.java:241)
    at java.lang.ref.Reference$ReferenceHandler.run([email protected]/Reference.java:213)

"Finalizer" #3 daemon prio=8 os_prio=31 cpu=1.05ms elapsed=127.37s tid=0x00007f9ac081c000 nid=0x4903 in Object.wait()  [0x000070000d3d4000]
   java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait([email protected]/Native Method)
    - waiting on <no object reference available>
    at java.lang.ref.ReferenceQueue.remove([email protected]/ReferenceQueue.java:155)
    - locked <0x000000070055f0b0> (a java.lang.ref.ReferenceQueue$Lock)
    at java.lang.ref.ReferenceQueue.remove([email protected]/ReferenceQueue.java:176)
    at java.lang.ref.Finalizer$FinalizerThread.run([email protected]/Finalizer.java:170)

"Signal Dispatcher" #4 daemon prio=9 os_prio=31 cpu=0.26ms elapsed=127.36s tid=0x00007f9ac0805800 nid=0x3d07 runnable  [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread0" #5 daemon prio=9 os_prio=31 cpu=1884.06ms elapsed=127.36s tid=0x00007f9ac1803000 nid=0x5603 waiting on condition  [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE
   No compile task

"C1 CompilerThread0" #8 daemon prio=9 os_prio=31 cpu=1066.76ms elapsed=127.36s tid=0x00007f9ac1804800 nid=0x5803 waiting on condition  [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE
   No compile task

"Sweeper thread" #9 daemon prio=9 os_prio=31 cpu=11.33ms elapsed=127.36s tid=0x00007f9ac1808000 nid=0xa603 runnable  [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"Service Thread" #10 daemon prio=9 os_prio=31 cpu=0.07ms elapsed=127.34s tid=0x00007f9ac1821000 nid=0x5a03 runnable  [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"Common-Cleaner" #11 daemon prio=8 os_prio=31 cpu=1.22ms elapsed=127.34s tid=0x00007f9ac088f000 nid=0xa303 in Object.wait()  [0x000070000dae9000]
   java.lang.Thread.State: TIMED_WAITING (on object monitor)
    at java.lang.Object.wait([email protected]/Native Method)
    - waiting on <no object reference available>
    at java.lang.ref.ReferenceQueue.remove([email protected]/ReferenceQueue.java:155)
    - locked <0x000000070057a0f8> (a java.lang.ref.ReferenceQueue$Lock)
    at jdk.internal.ref.CleanerImpl.run([email protected]/CleanerImpl.java:148)
    at java.lang.Thread.run([email protected]/Thread.java:835)
    at jdk.internal.misc.InnocuousThread.run([email protected]/InnocuousThread.java:134)

"AppKit Thread" #12 daemon prio=5 os_prio=31 cpu=3875.45ms elapsed=127.25s tid=0x00007f9ac010a000 nid=0x307 runnable  [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"AWT-Shutdown" #13 prio=5 os_prio=31 cpu=0.19ms elapsed=127.22s tid=0x00007f9ac010c000 nid=0x8c03 in Object.wait()  [0x000070000dc6f000]
   java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait([email protected]/Native Method)
    - waiting on <no object reference available>
    at java.lang.Object.wait([email protected]/Object.java:326)
    at sun.awt.AWTAutoShutdown.run([email protected]/AWTAutoShutdown.java:291)
    - locked <0x000000070055f0f0> (a java.lang.Object)
    at java.lang.Thread.run([email protected]/Thread.java:835)

"AWT-EventQueue-0" #14 prio=6 os_prio=31 cpu=2127.72ms elapsed=126.82s tid=0x00007f9abfa00800 nid=0xd003 waiting on condition  [0x000070000e001000]
   java.lang.Thread.State: WAITING (parking)
    at jdk.internal.misc.Unsafe.park([email protected]/Native Method)
    - parking to wait for  <0x000000070058ae30> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
    at java.util.concurrent.locks.LockSupport.park([email protected]/LockSupport.java:194)
    at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await([email protected]/AbstractQueuedSynchronizer.java:2081)
    at java.awt.EventQueue.getNextEvent([email protected]/EventQueue.java:566)
    at java.awt.EventDispatchThread.pumpOneEventForFilters([email protected]/EventDispatchThread.java:190)
    at java.awt.EventDispatchThread.pumpEventsForFilter([email protected]/EventDispatchThread.java:124)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy([email protected]/EventDispatchThread.java:113)
    at java.awt.EventDispatchThread.pumpEvents([email protected]/EventDispatchThread.java:109)
    at java.awt.EventDispatchThread.pumpEvents([email protected]/EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.run([email protected]/EventDispatchThread.java:90)

"DestroyJavaVM" #15 prio=5 os_prio=31 cpu=173.20ms elapsed=126.82s tid=0x00007f9ac2011800 nid=0x1e03 waiting on condition  [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"Java2D Queue Flusher" #16 daemon prio=10 os_prio=31 cpu=599.14ms elapsed=126.53s tid=0x00007f9ac0187000 nid=0x11e07 in Object.wait()  [0x000070000e104000]
   java.lang.Thread.State: TIMED_WAITING (on object monitor)
    at java.lang.Object.wait([email protected]/Native Method)
    - waiting on <no object reference available>
    at sun.java2d.opengl.OGLRenderQueue$QueueFlusher.run([email protected]/OGLRenderQueue.java:205)
    - locked <0x000000070055f568> (a sun.java2d.opengl.OGLRenderQueue$QueueFlusher)
    at java.lang.Thread.run([email protected]/Thread.java:835)

"Java2D Disposer" #17 daemon prio=10 os_prio=31 cpu=0.94ms elapsed=126.47s tid=0x00007f9ac01b1000 nid=0x11717 in Object.wait()  [0x000070000e207000]
   java.lang.Thread.State: WAITING (on object monitor)
    at java.lang.Object.wait([email protected]/Native Method)
    - waiting on <no object reference available>
    at java.lang.ref.ReferenceQueue.remove([email protected]/ReferenceQueue.java:155)
    - locked <0x000000070055f768> (a java.lang.ref.ReferenceQueue$Lock)
    at java.lang.ref.ReferenceQueue.remove([email protected]/ReferenceQueue.java:176)
    at sun.java2d.Disposer.run([email protected]/Disposer.java:144)
    at java.lang.Thread.run([email protected]/Thread.java:835)

"TimerQueue" #22 daemon prio=5 os_prio=31 cpu=23.30ms elapsed=120.53s tid=0x00007f9abfc2f000 nid=0x15537 waiting on condition  [0x000070000e30a000]
   java.lang.Thread.State: WAITING (parking)
    at jdk.internal.misc.Unsafe.park([email protected]/Native Method)
    - parking to wait for  <0x0000000700551ee8> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
    at java.util.concurrent.locks.LockSupport.park([email protected]/LockSupport.java:194)
    at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await([email protected]/AbstractQueuedSynchronizer.java:2081)
    at java.util.concurrent.DelayQueue.take([email protected]/DelayQueue.java:217)
    at javax.swing.TimerQueue.run([email protected]/TimerQueue.java:171)
    at java.lang.Thread.run([email protected]/Thread.java:835)

"SwingWorker-pool-1-thread-1" #23 daemon prio=5 os_prio=31 cpu=189.72ms elapsed=105.85s tid=0x00007f9ac02f3800 nid=0x7e4b waiting on condition  [0x000070000dd72000]
   java.lang.Thread.State: WAITING (parking)
    at jdk.internal.misc.Unsafe.park([email protected]/Native Method)
    - parking to wait for  <0x00000007005604b8> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
    at java.util.concurrent.locks.LockSupport.park([email protected]/LockSupport.java:194)
    at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await([email protected]/AbstractQueuedSynchronizer.java:2081)
    at java.util.concurrent.LinkedBlockingQueue.take([email protected]/LinkedBlockingQueue.java:433)
    at java.util.concurrent.ThreadPoolExecutor.getTask([email protected]/ThreadPoolExecutor.java:1054)
    at java.util.concurrent.ThreadPoolExecutor.runWorker([email protected]/ThreadPoolExecutor.java:1114)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run([email protected]/ThreadPoolExecutor.java:628)
    at java.lang.Thread.run([email protected]/Thread.java:835)

"HttpClient-1-SelectorManager" #24 daemon prio=5 os_prio=31 cpu=38.46ms elapsed=105.78s tid=0x00007f9ac00da800 nid=0x1fc07 runnable  [0x000070000eb22000]
   java.lang.Thread.State: RUNNABLE
    at sun.nio.ch.KQueue.poll([email protected]/Native Method)
    at sun.nio.ch.KQueueSelectorImpl.doSelect([email protected]/KQueueSelectorImpl.java:122)
    at sun.nio.ch.SelectorImpl.lockAndDoSelect([email protected]/SelectorImpl.java:124)
    - locked <0x000000070f61cec0> (a sun.nio.ch.Util$2)
    - locked <0x000000070f61ce60> (a sun.nio.ch.KQueueSelectorImpl)
    at sun.nio.ch.SelectorImpl.select([email protected]/SelectorImpl.java:136)
    at jdk.internal.net.http.HttpClientImpl$SelectorManager.run([email protected]/HttpClientImpl.java:867)

"Attach Listener" #28 daemon prio=9 os_prio=31 cpu=0.97ms elapsed=0.10s tid=0x00007f9ac19ce000 nid=0x16007 waiting on condition  [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"VM Thread" os_prio=31 cpu=27.33ms elapsed=127.37s tid=0x00007f9abf908800 nid=0x3703 runnable  

"GC Thread#0" os_prio=31 cpu=24.36ms elapsed=127.38s tid=0x00007f9abf839000 nid=0x2d03 runnable  

"GC Thread#1" os_prio=31 cpu=18.07ms elapsed=105.83s tid=0x00007f9ac188b800 nid=0x28fb runnable  

"GC Thread#2" os_prio=31 cpu=22.84ms elapsed=105.83s tid=0x00007f9ac188e800 nid=0x7a0f runnable  

"GC Thread#3" os_prio=31 cpu=20.48ms elapsed=105.83s tid=0x00007f9ac19cf800 nid=0x7f07 runnable  

"GC Thread#4" os_prio=31 cpu=23.51ms elapsed=105.83s tid=0x00007f9ac288c800 nid=0x9407 runnable  

"GC Thread#5" os_prio=31 cpu=20.98ms elapsed=105.83s tid=0x00007f9abf97c000 nid=0x11213 runnable  

"G1 Main Marker" os_prio=31 cpu=0.23ms elapsed=127.38s tid=0x00007f9abf852000 nid=0x4f03 runnable  

"G1 Conc#0" os_prio=31 cpu=0.03ms elapsed=127.38s tid=0x00007f9abf852800 nid=0x3003 runnable  

"G1 Refine#0" os_prio=31 cpu=5.64ms elapsed=127.38s tid=0x00007f9abf8f3800 nid=0x4d03 runnable  

"G1 Refine#1" os_prio=31 cpu=2.59ms elapsed=105.82s tid=0x00007f9ac3059800 nid=0x8307 runnable  

"G1 Refine#2" os_prio=31 cpu=0.90ms elapsed=105.82s tid=0x00007f9ac2875800 nid=0x1090b runnable  

"G1 Young RemSet Sampling" os_prio=31 cpu=22.63ms elapsed=127.38s tid=0x00007f9abf8f4000 nid=0x3303 runnable  
"VM Periodic Task Thread" os_prio=31 cpu=86.51ms elapsed=127.34s tid=0x00007f9ac0019800 nid=0x5c03 waiting on condition  

JNI global refs: 113, weak refs: 252

Any suggestions welcome.


Solution

  • It does not block. Note that there is no doInBackground method in the whole thread dump, and SwingWorker thread pool is idle.

    More likely, the code throws an uncaught exception or error - presumably, NoClassDefFoundError. Some dependencies must be missing in the classpath when you launch the application as -jar.