1

I'm not sure why I am having this issue. Here's what I'm running on linux command line:

sudo java -jar gs-rest-service-0.1.0.jar -cp ./hello
java.lang.ClassNotFoundException: hello.Application

sudo java -jar gs-rest-service-0.1.0.jar -cp hello
java.lang.ClassNotFoundException: hello.Application

Tried it both ways, with the ./ and without. Also tried it like this:

sudo java -jar gs-rest-service-0.1.0.jar -cp ./main/java/hello
java.lang.ClassNotFoundException: hello.Application

Again with and without the ./. When I view the jar file I see the Application.class file in main/java/hello all lower case. Here's my MANIFEST.MF

Manifest-Version: 1.0
Class-Path: ./hello

Update: Got it working. Here I had an issue in my pom.xml file I had specified start-class as hello.Application but my package names I kept messing around with so they were never in sync and adding the -cp wasn't helping since now I would have package one thing in the classes, start-class in the pom.xml another, and -cp even in the wrong location on the command line! Argh.. Just a dumb night.

Here's the working code:

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.springframework</groupId>
<artifactId>gs-rest-service</artifactId>
<version>0.1.0</version>
<packaging>jar</packaging>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.0.2.RELEASE</version>
</parent>

<dependencies>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    </dependency>
</dependencies>

<properties>
    <start-class>main.java.hello.Application</start-class>
</properties>

<build>
    <plugins>
    <plugin> 
        <artifactId>maven-compiler-plugin</artifactId> 
        <version>2.3.2</version> 
    </plugin>
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
    </plugins>
</build>

<repositories>
    <repository>
    <id>spring-snapshots</id>
    <url>http://repo.spring.io/libs-snapshot</url>
    <snapshots><enabled>true</enabled></snapshots>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
    <id>spring-snapshots</id>
    <url>http://repo.spring.io/libs-snapshot</url>
    <snapshots><enabled>true</enabled></snapshots>
    </pluginRepository>
</pluginRepositories>
</project>

Application.java:

package main.java.hello;


import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan
@EnableAutoConfiguration
public class Application {

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

All other java files have the same package name specified and used in the eclipse IDE. To get rid of the IDE showing red X's on everything I ran maven with this param:

mvn dependency:copy-dependencies

As described in this wonderful question here:

Downloading all maven dependencies to a directory NOT in repository?

After downloaded I created a user library and pulled all jar files from the dependency directory that's created after maven does it's download thing. Thought I'd add that into the question. A bit out of scope but was helpful to get rid of further errors in the IDE that show up when editing a stupid simple hello world web service.

Community
  • 1
  • 1
JKK
  • 436
  • 2
  • 11
  • 22
  • looked at the referenced question by Jarrod Roberson but was very little info, didn't include code or manifest contents. Don't see how its a good replacement but that's ok. Maybe my little brain just lacks the power to get it – JKK Jun 03 '14 at 00:43

3 Answers3

1

You need to specify a main class.

sudo java -cp gs-rest-service-0.1.0.jar -cp ./main/java hello.Application

The -jar flag tells Java to run the main class referenced in that jar.

David Ehrmann
  • 7,366
  • 2
  • 31
  • 40
  • Updated question to include the application.java file. it has static main specified in it. Still no go, tried to run your statement there – JKK Jun 03 '14 at 00:19
  • Updated question again to include results. oh duh, I have a slash in there – JKK Jun 03 '14 at 00:23
  • @JKK I also didn't do ```-jar```. I'd get the boring version working before trying to get the manifest to work. – David Ehrmann Jun 03 '14 at 00:27
  • sudo java -cp gs-rest-service-0.1.0.jar -cp ./main/java hello.Application Error: Could not find or load main class hello.Application – JKK Jun 03 '14 at 00:36
  • Is there a file named Application.class in ./main/java/hello? – David Ehrmann Jun 03 '14 at 01:01
  • yes, I have the jar file open in Ark, there's directory structure: /main/java/hello and files in there: Application.class (contains static void main), Greeting.class, GreetingController.class – JKK Jun 03 '14 at 01:32
  • @JKK In the example I saw, you did ```...-cp ./main/java/hello```, not ```... -cp ./main/java hello.Application```. – David Ehrmann Jun 03 '14 at 04:02
  • Got it working, was a conflict I found in the pom.xml where I had the start-class param setup and didn't notice it. Was a bad night for me. Thank you for responding back and forth trying to help – JKK Jun 04 '14 at 00:27
  • Hey David, upvote the question if you get a chance to counter the duplicate question downvote above. His linked question is worthless didn't help at all. – JKK Jun 04 '14 at 00:36
0

You add the jar to the classpath, and run it like this -

sudo java -cp gs-rest-service-0.1.0.jar hello.Application

If you can add a "Main-Class" entry to the jar file MANIFEST.MF,

Main-Class: hello.Application

Then you can also run it without specifying "hello.Application" (using "-jar") -

sudo java -jar gs-rest-service-0.1.0.jar
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

I'm guessing a bit, here, but seeing the Spring annotations and its ability to automagically find classes,

sudo java -cp ./main/java -jar gs-rest-service-0.1.0.jar 
David Ehrmann
  • 7,366
  • 2
  • 31
  • 40