Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Guice caused throwing UncaughtExceptionHandler

Tags:

java

guice

I would like to use Guice in my application but it throws following exception when I want to run it:

Exception in thread "main" 
Exception: com.google.common.util.concurrent.UncheckedExecutionException thrown from the UncaughtExceptionHandler in thread "main"

When I wrap main() function with try... catch block I receive folloving stackTrace:

Exception in thread "main" com.google.common.util.concurrent.UncheckedExecutionException: java.lang.IllegalArgumentException
    at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2047)
    at com.google.common.cache.LocalCache.get(LocalCache.java:3869)
    at com.google.common.cache.LocalCache.getOrLoad(LocalCache.java:3873)
    at com.google.common.cache.LocalCache$LocalLoadingCache.get(LocalCache.java:4798)
    at com.google.common.cache.LocalCache$LocalLoadingCache.getUnchecked(LocalCache.java:4804)
    at com.google.inject.internal.util.StackTraceElements.forMember(StackTraceElements.java:71)
    at com.google.inject.internal.Messages.formatSource(Messages.java:241)
    at com.google.inject.internal.Messages.formatSource(Messages.java:220)
    at com.google.inject.internal.Messages.formatMessages(Messages.java:90)
    at com.google.inject.CreationException.getMessage(CreationException.java:50)
    at java.base/java.lang.Throwable.getLocalizedMessage(Throwable.java:396)
    at java.base/java.lang.Throwable.toString(Throwable.java:485)
    at java.base/java.lang.String.valueOf(String.java:2788)
    at java.base/java.io.PrintStream.println(PrintStream.java:897)
    at java.base/java.lang.Throwable$WrappedPrintStream.println(Throwable.java:752)
    at java.base/java.lang.Throwable.printStackTrace(Throwable.java:659)
    at java.base/java.lang.Throwable.printStackTrace(Throwable.java:648)
    at java.base/java.lang.Throwable.printStackTrace(Throwable.java:639)
    at com.github.blackjack.Main.main(Main.java:31)
Caused by: java.lang.IllegalArgumentException
    at com.google.inject.internal.asm.$ClassReader.<init>(ClassReader.java:160)
    at com.google.inject.internal.asm.$ClassReader.<init>(ClassReader.java:143)
    at com.google.inject.internal.asm.$ClassReader.<init>(ClassReader.java:418)
    at com.google.inject.internal.util.LineNumbers.<init>(LineNumbers.java:64)
    at com.google.inject.internal.util.StackTraceElements$1.load(StackTraceElements.java:49)
    at com.google.inject.internal.util.StackTraceElements$1.load(StackTraceElements.java:45)
    at com.google.common.cache.LocalCache$LoadingValueReference.loadFuture(LocalCache.java:3443)
    at com.google.common.cache.LocalCache$Segment.loadSync(LocalCache.java:2169)
    at com.google.common.cache.LocalCache$Segment.lockedGetOrLoad(LocalCache.java:2128)
    at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2041)
    ... 18 more

From the information I found this exception was caused by method contains a lambda definition, is thrown by Provides method. Nevertheless it should be fixed with Guice 4.0. Maybe someone know how to fix it and can help? Thanks in advance!

main() function:

public static void main (String[] args){
    try{
        MainModule module = new MainModule();
        Injector injector = Guice.createInjector(module);

        GameFactory gameFactory = injector.getInstance(GameFactory.class);
        DeckFactory blackjackDeckFactory = injector.getInstance(BlackjackDeckCreator.class);
        PlayerFactory playerFactory = injector.getInstance(PlayerFactory.class);

        Player player = playerFactory.createPlayer("Paul");
        Player dealer = playerFactory.createPlayer("Dealer", true);

        Deck deck = blackjackDeckFactory.createDeck();

        Game game = gameFactory.createGame(player, dealer, deck);
        game.start();
    } catch (Exception e){
        e.printStackTrace(); // line 31
    }
}
like image 600
wege Avatar asked Jan 28 '26 19:01

wege


1 Answers

I use Java 10, but also tried 8 and 9. Nothing helped.

I think you didn't use Java 8 or 9 because the exception is thrown from ClassReader due to using Java version 10 or later .

https://gitlab.ow2.org/asm/asm/blob/f7f44a8e306dd2bd70f0db50194f6366938579c5/src/org/objectweb/asm/ClassReader.java#L160

Opcodes.V9 means Java 9. So please check your Java version again and run with Java 9 or earlier.

like image 149
Kohei TAMURA Avatar answered Jan 30 '26 10:01

Kohei TAMURA



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!