1

I'm not a Java dev and am unfamiliar with the packaging and building of Java programs. I'm trying to run this file: https://github.com/CodinGame/SpringChallenge2020/blob/master/src/test/java/Spring2020Main.java

by doing

mvn clean install
java -jar .\target\spring-2020-1.0-SNAPSHOT.jar

but I get this error:

no main manifest attribute, in .\target\spring-2020-1.0-SNAPSHOT.jar

I can't figure out for the life of me what I need to add to the pom.xml or whatever else I need to do to get this to work.

Any help will be appreciated.

snowfrogdev
  • 5,963
  • 3
  • 31
  • 58
  • 1
    The file you've linked is in the src/main/test directory. Are you trying to run a test? Why cannot the same file be in src/main/java directory under any of the packages that you have created? – kayvis May 08 '20 at 14:24
  • 1
    The class you are looking for is a test. It is best to run it from your Eclipse or IntelliJ (whatever you are using). – J Fabian Meier May 08 '20 at 14:25
  • @kayvis I don't have a problem moving it if it would help. – snowfrogdev May 08 '20 at 14:47
  • @JFMeier I intend to modify this file slightly and run it from the command line, so running it from an IDE is not an option. – snowfrogdev May 08 '20 at 14:48

3 Answers3

2

You can follow the steps below:

  1. Move Spring2020Main.java to src/main/java/com/codingame directory
  2. Add the following to your pom.xml after the </dependencies>:
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.2.3</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>com.codingame.Spring2020Main</mainClass>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>    
  1. Run maven build using mvn clean install
  2. Execute the program using java -jar target/spring-2020-1.0-SNAPSHOT.jar

Info: Apache Maven Shade Plugin helps in building what is called an uber-jar or a fat-jar. This means that all the dependencies are packaged as part of the resultant jar file without the need for any 'libraries' that you'd need to add in the classpath when executing the jar file. As part of the final jar, we need to specify which file needs to be treated as the main file to be executed. This is typically done using META-INF/MANIFEST.MF file inside the uber-jar. That's what the transformer specified inside the configuration of the plugin does for us.

kayvis
  • 563
  • 2
  • 9
2

A few things to understand about Java:

1) If you have a Maven project like this, code is divided between src/main/ and src/test/ directories. src/test/ is intended for unit tests. In your case, Spring2020Main is not actually set up as a unit test, so I'm not sure what the author intended here.

2) When you compile using mvn clean install, a jar (library) is built, but nothing from src/test will be included in the output.

Generally, tests are executed during build. And this one would have been, except it's not set up as a real junit test, so it didn't run during build.

3) You can move the file from src/test/java to src/main/java and it will be built into your resulting jar.

4) In this case, when you run the JVM, you need to specify a classpath. This is a list of all libraries to include when the application starts. You also need to specify the (fully qualified) name of the class to run:

java -cp target/spring-2020-1.0-SNAPSHOT.jar Spring2020Main

...the above won't work directly since there are more unsatisfied dependencies (the top level pom.xml brings in at least 3 other deps you'd also need to provide on the classpath).

As others pointed out, a solution could be to build a self-executing jar, but simplest for you would be to run this from an IDE:

Run this from IntelliJ. If you haven't installed it, install it.

1) File > New From Existing Sources, find the directory where this is coned to.

2) When asked, Import Project from Existing Model (Maven)

3) When the Project view is available (alt-1), or View > Tool Windows > Project, you can expand the structure till you find Spring2020Main in the test directory.

4) Right-click it and select Run.

For me, it exposed a web server running at http://localhost:8888/test.html

cbmatthews
  • 36
  • 2
  • Thanks for the explanation. It helped me understand a bit better how things are organized. The problem is that I plan to modify that file to take in some arguments and I need to run it from the command line. – snowfrogdev May 08 '20 at 18:02
1

The project you've linked has only a basic setup for compilation (that would be enough to run it from IDE though).

What you need is an executable jar. Check this thread.

As others mentioned (and I failed to notice) the class you linked to is a test class, so it may not be included in a jar by default. Run it through IDE or set it up in a proper source directory.

Vic
  • 308
  • 2
  • 7