0

I have a barebones project configured with maven and dropwizard. When I run the command java -jar target/dexcane-backend-1.0-SNAPSHOT.jar I get the error "Error: Could not find or load main class com.dexcane.server.MyApplication". I get the same error if I try to specify the classpath using java -cp target/dexcane-backend-1.0-SNAPSHOT.jar com.dexcane.server.MyApplication.

When I check the contents of the jar using jar tf, I get

META-INF/
META-INF/MANIFEST.MF
com/
com/dexcane/
com/dexcane/server/
com/dexcane/server/configuration/
com/dexcane/server/resources/
META-INF/maven/
META-INF/maven/org.example/
META-INF/maven/org.example/dexcane-backend/
config.yml
com/dexcane/server/configuration/WebConfiguration.class
com/dexcane/server/resources/UsersResource.class
com/dexcane/server/resources/HelloWorldResource.class
com/dexcane/server/MyApplication.class
META-INF/maven/org.example/dexcane-backend/pom.xml
META-INF/maven/org.example/dexcane-backend/pom.properties

I'm not sure why the jar can't find the main class? FWIW, my main class extends the io.dropwizard.Application class.

package com.dexcane.server;

import io.dropwizard.Application;
import io.dropwizard.setup.Environment;
import com.dexcane.server.configuration.WebConfiguration;
import com.dexcane.server.resources.HelloWorldResource;

public class MyApplication extends Application<WebConfiguration> {
    public static void main(String[] args) throws Exception {
        new MyApplication().run(args);
    }

    @Override
    public void run(WebConfiguration webConfiguration, Environment environment) throws Exception {
        final HelloWorldResource resource = new HelloWorldResource();
        environment.jersey().register(resource);
    }
}
Victor Cui
  • 1,393
  • 2
  • 15
  • 35
  • the package line is missing . Do you have 'package com.dexcane.server' ? . – Pierre Mar 19 '23 at 21:14
  • You need to configure the main class and ensure it's written to the jar's manifest file - [for example](https://stackoverflow.com/questions/29920434/maven-adding-mainclass-in-pom-xml-with-the-right-folder-path) – MadProgrammer Mar 19 '23 at 21:21
  • @pierre yes I have the package line. – Victor Cui Mar 19 '23 at 21:44
  • 1
    Is `Error: Could not find or load main class com.dexcane.server.MyApplication` the whole error message? I'd expect a second line `Caused by: java.lang.NoClassDefFoundError: io.dropwizard.Application`. You can't load a class if it's parent classes are not on the classpath. – tgdavies Mar 19 '23 at 21:59
  • @tgdavies I don't see the `NoClassDefFoundError` – Victor Cui Mar 19 '23 at 23:27
  • 1
    Try adding your dependencies to the classpath. – tgdavies Mar 19 '23 at 23:42

1 Answers1

0

I don't understand exactly how this fixed my error but following the accepted answer here resolved my error and allowed my to run the jar

Victor Cui
  • 1,393
  • 2
  • 15
  • 35