#[SOLVED] JAR Does not want to run

1 messages Β· Page 1 of 1 (latest)

frosty pivot
#

Hi!

I'm having an issue with an app, if I run my main file from my IDE (Intellij) it works perfectly fine. The issue is that whenever I build a JAR file from intellij that jar file throws an exception and I just can not figure out why.

Can someone point me in the right direction on how to debug this?

The exception in question:

[main] WARN org.springframework.context.annotation.AnnotationConfigApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'main': Unsatisfied dependency expressed through constructor parameter 1: Error creating bean with name 'manager' defined in URL [jar:file:/C:/Users/MAD/Documents/projects/java/FrigateManager/out/artifacts/FrigateManager_jar/FrigateManager.jar!/org/altered/frigate_manager/Manager.class]: null
[main] ERROR org.springframework.boot.SpringApplication - Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'main': Unsatisfied dependency expressed through constructor parameter 1: Error creating bean with name 'manager' defined in URL [jar:file:/C:/Users/MAD/Documents/projects/java/FrigateManager/out/artifacts/FrigateManager_jar/FrigateManager.jar!/org/altered/frigate_manager/Manager.class]: null
        at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:795)
        at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:237)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1375)
        at 
....

So it seems to me that it cannot instantiate the "org.altered.frigate_manager.Manager" class in spring's DI.

cedar surgeBOT
#

<@&1004656351647117403> please have a look, thanks.

sharp raven
frosty pivot
#

Sure thing!

Here is the output of trying to run the jar manually with "java -jar FrigateManager.jar"

And the contents of the Manager class

package org.altered.frigate_manager;

import org.altered.frigate_manager.commands.NotificationDaemonCommand;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Component;
import picocli.CommandLine;

import java.util.concurrent.Callable;

@Component
@CommandLine.Command(
    name="manage",
    mixinStandardHelpOptions = true,
    description = "Manage the configured frigate instance",
    subcommands = {
        NotificationDaemonCommand.class,
    }
)
public class Manager implements Callable<Integer> {

    protected static final Logger logger = LogManager.getLogger();

    @Override
    public Integer call() throws Exception {
        logger.info("Hello!");
        return 0;
    }
}
cedar surgeBOT
sharp raven
#

Caused by: java.lang.UnsupportedOperationException: No class provided, and an appropriate one cannot be found.
at org.apache.logging.log4j.LogManager.callerClass(LogManager.java:585)
at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:610)
at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:597)
at org.altered.frigate_manager.Manager.<clinit>(Manager.java:22)

#

seems like a logger problem

#

@frosty pivot show Manager please

#

ah thank you

frosty pivot
#

@sharp raven The previous message contains the contents of Manager.java

Here is the main file as well to provide more context:

package org.altered;

import org.altered.frigate_manager.Manager;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.ExitCodeGenerator;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import picocli.CommandLine;
import picocli.CommandLine.IFactory;


@SpringBootApplication
public class Main implements CommandLineRunner, ExitCodeGenerator {

    private int exitCode;

    private final IFactory factory;

    Main(IFactory factory, Manager manager) {
        this.factory = factory;
    }

    public static void main(String[] args) {
        System.exit(SpringApplication.exit(SpringApplication.run(Main.class, args)));
    }

    @Override
    public void run(String... args) throws Exception {
        exitCode = new CommandLine(Manager.class, factory).execute(args);
    }

    @Override
    public int getExitCode()
    {
        return exitCode;
    }
}
cedar surgeBOT
sharp raven
#
protected static final Logger logger = LogManager.getLogger();

Yes you are not providing any class to the logger

sharp raven
#

or something like this

frosty pivot
#

@sharp raven Thanks! I have set the classtype to the logger and now it works. But my question still remains, why would it work within the IDE and not in the JAR when its not declared?

cedar surgeBOT
#

@frosty pivot

Your question has been closed due to inactivity.

If it was not resolved yet, feel free to just post a message below
to reopen it, or create a new thread.

Note that usually the reason for nobody calling back is that your
question may have been not well asked and hence no one felt confident
enough answering.

When you reopen the thread, try to use your time to improve the quality
of the question by elaborating, providing details, context, all relevant code
snippets, any errors you are getting, concrete examples and perhaps also some
screenshots. Share your attempt, explain the expected results and compare
them to the current results.

Also try to make the information easily accessible by sharing code
or assignment descriptions directly on Discord, not behind a link or
PDF-file; provide some guidance for long code snippets and ensure
the code is well formatted and has syntax highlighting. Kindly read through
https://stackoverflow.com/help/how-to-ask for more.

With enough info, someone knows the answer for sure πŸ‘

quick harness
frosty pivot
#

@quick harness True! But why would it work when I change the function argument?

Ahh well, anyway! Its working now which is great 😁

quick harness
#

this logger detection under the hood is some real black magic voodoo sh*t ^^

#

there are mechanisms checking for certain class names in your classpath and then executing certain logic because of that

frosty pivot
#

Yeah I noticed! Its a ton of interfaces and such.

This is the line in question:
protected static final Logger logger = LogManager.getLogger();

#

If I make it like getLogger(ClassName.class) it works fine.

I traced the issue to be with that it tries to determine the callingclass through a stacktrace and thats when if fails in the jar

#

So maybe that logic is provided by the IDE? I don't know. @quick harness

quick harness
#

getLogger(ClassName.class) this is the usual way o.o

#

there's shouldn't even be a getLogger() method :x

frosty pivot
#

But there is! And in the IDE environment it works fine. The method tries to access the stack, inverse it and determine the calling class but in the jar that failed thus causing the confusing error

quick harness
#

which logger implementation are you using?

frosty pivot
#

I'd have to check when I'm at my pc again πŸ˜… but it was some default slf4j stuff (before it was log4j

quick harness
#

yaaa, this one has a special getLogger() method xD

#

returns the logger of the calling class πŸ€”

#

I guess this is also caused by your depending jar thingy; but I'm not sure how the magic of getting the calling class works under the hood

#

if you would have built a fat jar; this might work

frosty pivot
#

I did! I built every dependency into the jar

quick harness
#

then you wouldn't have to add your other jar to the classpath ^^

#

I just wouldn't use this method and use getLogger(ClassName.class) instead xD

#

or use lombok with logback or so ^^

frosty pivot
#

I'm using JDK 21 so I dont know if its still applicable?

Never heard about that property though

quick harness
#

I'm always using logback for logging πŸ˜„

frosty pivot
#

I removed log4j and I'm using the slf4j stuff now

#

I have much to learn! 😁

quick harness
#

keep making errors; and you keep learning ^^

frosty pivot
#

Except this wasnt technically my error :p I was reading functioncalls and descriptions

quick harness
#

it's always a layer 8 problem πŸ˜„

frosty pivot
#

Fair enough! Years of experience with other languages and decided to go for java now. And honestly? Not dissappointed!

tranquil vector
#

i havent read through the older posts, were you able to solve this?

frosty pivot
#

Thank you @tranquil vector ! And also, yes! It has been resolved.

The issue was with how a logger was instantiated. When no argument was given it tried to instantiate based upon the stack and determine which class called it so it could use that class as a name. That did not work in the built jar file

#

[SOLVED] JAR Does not want to run

flint hound
frosty pivot
#

@flint hound Perfect, thank you!