Simply starting specifying the classpath solved the problem:
java -cp target/myjar.jar myMainClass
Question
I created my first web application following the Jersey user guide with the artifact jersey-quickstart-grizzly2. Everything seems to work as expected: by starting my project using maven (mvn java:exec) or by starting from eclipse I can call my REST API from C++, Javascript etc...
Now I would like to run this web application on another platform, so I used the maven-assembly-plugin to create a jar containing whatever I need to run the same application. I create the package using the command:
mvn clean compile assembly:single
and my jar is compiled. If I try to run it with:
java -jar target/myjar.jar
it seems to start appropriately. If I call my REST API from any client, I see logs indicating that my methods are invoked as expected, but the response is always 500. This is the method invoked for instance:
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<MTTask> getTasks() {
MTLog.info("Processing GET request.");
return new LinkedList<MTTask>();
}
This is the output of curl:
$ curl -X GET -v http://localhost:8080/myapp/tasks
* Adding handle: conn: 0x11d4c30
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x11d4c30) send_pipe: 1, recv_pipe: 0
* About to connect() to localhost port 8080 (#0)
* Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET /myapp/tasks HTTP/1.1
> User-Agent: curl/7.32.0
> Host: localhost:8080
> Accept: */*
>
< HTTP/1.1 500 Request failed.
< Content-Type: text/html;charset=ISO-8859-1
< Date: Fri, 21 Mar 2014 22:31:11 GMT
< Connection: close
< Content-Length: 1031
<
<html><head><title>Grizzly 2.3.8</title><style><!--div.header {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#003300;font-size:22px;-moz-border-radius-topleft: 10px;border-top-left-radius: 10px;-moz-border-radius-topright: 10px;border-top-right-radius: 10px;padding-left: 5px}div.body {font-family:Tahoma,Arial,sans-serif;color:black;background-color:#FFFFCC;font-size:16px;padding-top:10px;padding-bottom:10px;padding-left:10px}div.footer {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#666633;font-size:14px;-moz-border-radius-bottomleft: 10px;border-bottom-left-radius: 10px;-moz-border-radius-bottomright: 10px;border-bottom-right-radius: 10px;padding-left: 5px}BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;}B {font-family:Tahoma,Arial,sans-serif;color:black;}A {color : black;}HR {color : #999966;}--></style> </head><body><div class="header">Request failed.</div><div class="body">Request failed.</div><div class="footer">Grizzly 2.3.8</div></body>* Closing connection 0
</html>
If instead I run the same code with mvn java:exec on the same location and same machine, this is the output:
$ curl -X GET -v http://localhost:8080/myapp/tasks
* Adding handle: conn: 0x17a2c30
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x17a2c30) send_pipe: 1, recv_pipe: 0
* About to connect() to localhost port 8080 (#0)
* Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET /myapp/tasks HTTP/1.1
> User-Agent: curl/7.32.0
> Host: localhost:8080
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Type: application/json
< Date: Fri, 21 Mar 2014 22:33:23 GMT
< Content-Length: 2
<
* Connection #0 to host localhost left intact
[]
As expected. So my question is: what is the difference? Can I somehow create that jar to deploy on my server in a similar simple and quick way?
EDIT: This is the build portion of my pom file:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.luke.Main</mainClass>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.luke.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Solution
Simply starting specifying the classpath solved the problem:
java -cp target/myjar.jar myMainClass