Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Running a jar file on command line, Maven build

  • 25-08-2014 10:45pm
    #1
    Registered Users, Registered Users 2 Posts: 6,262 ✭✭✭


    I've written a little server side app in Java, with a few more elements need adding to it.

    I can run the program no problem in my IDE (Eclipse) using different configurations and what not, passing different arguments via the configuration editor.

    However, once I build the program with maven, the jar file that is outputted fails to run. The jarfile name is twitter-0.0.1-SNAPSHOT.jar

    I can run the below equivalent configuration in the IDE and it runs fine
    java -jar twitter-0.0.1-SNAPSHOT.jar ryanair.properties
    
    but when I run it in on the command line I get the following stacktrace:
    Exception in thread "main" java.lang.NullPointerException
    	Exception in thread "main" java.lang.NoClassDefFoundError: org/jsoup/Jsoup
    	at com.airport.twitter.HtmlParser.arrivalsFetch(HtmlParser.java:28)
    	at com.airport.twitter.App.doArrivals(App.java:60)
    	at com.airport.twitter.App.main(App.java:34)
    Caused by: java.lang.ClassNotFoundException: org.jsoup.Jsoup
    	at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    	at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    	at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    

    Why will the app load the Jsoup API in the IDE and not from the packaged jar file?


Comments

  • Registered Users, Registered Users 2 Posts: 605 ✭✭✭Lmao_Man


    As far as I'm aware you're right, the external jars aren't there when you're running it from the command line.

    There's a few programs you can use to make fat jars which allow you to combine all the jars needed in to just one.

    Edi - try jarsplice. Worked for me recently for the same issue you were having..


  • Registered Users, Registered Users 2 Posts: 27,370 ✭✭✭✭GreeBo


    What other jars do you use (jsoup) and are they listed as dependencies in your pom.xml?
    Sounds a lot like you just have them as libraries in your ide.


  • Registered Users, Registered Users 2 Posts: 4,792 ✭✭✭cython


    This would be normal enough for a maven build unless you have specifically configured your pom.xml to package dependencies in your jar. Basically your IDE has a runtime classpath that might be configured internally, but could equally be derived from maven (depends on what IDE you are using and what integration you have between them). Maven makes dependencies available at compile and test time, but doesn't assume that it should include them in your own artifact, as depending on what you do with it afterwards, it may not be appropriate (e.g. package in a war or ear with other jars).

    Basically though, you will need to either have runtime dependencies available and add them to the classpath on the java command, or you should look at something like the shade plugin to build an "uber-jar" with all the dependency classes included


  • Registered Users, Registered Users 2 Posts: 27,370 ✭✭✭✭GreeBo


    Follow this OP


  • Registered Users, Registered Users 2 Posts: 6,262 ✭✭✭Buford T Justice


    Obvious questions are now obvious...

    I've not added the jar files in the IDE library (Eclipse). I've added them as maven dependencies instead. I thought that was the whole point of using maven.

    I'm using twitter4j and jsoup

    I've followed the link there (thanks for that) and I've tried it again. I've already had to add the mainClass configuration to the POM file, as the main class is called App.java and not Main.java

    Here's my current POM file
    <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>com.airport</groupId>
    	<artifactId>twitter</artifactId>
    	<version>0.0.1-SNAPSHOT</version>
    	<packaging>jar</packaging>
    
    	<name>twitter</name>
    	<url>http://maven.apache.org</url>
    
    	<properties>
    		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    	</properties>
    
    	<build>
    		<plugins>
    			<plugin>
    				<groupId>org.apache.maven.plugins</groupId>
    				<artifactId>maven-jar-plugin</artifactId>
    				<version>2.3.2</version>
    				<configuration>
    					<archive>
    						<manifest>
    							<addClasspath>true</addClasspath>
    							<mainClass>com.airport.twitter.App</mainClass>
    						</manifest>
    					</archive>
    				</configuration>
    			</plugin>
    			<plugin>
    				<artifactId>maven-assembly-plugin</artifactId>
    				<executions>
    					<execution>
    						<phase>package</phase>
    						<goals>
    							<goal>single</goal>
    						</goals>
    					</execution>
    				</executions>
    				<configuration>
    					<descriptorRefs>
    						<descriptorRef>jar-with-dependencies</descriptorRef>
    					</descriptorRefs>
    				</configuration>
    			</plugin>
    		</plugins>
    	</build>
    
    	<dependencies>
    		<dependency>
    			<groupId>junit</groupId>
    			<artifactId>junit</artifactId>
    			<version>3.8.1</version>
    			<scope>test</scope>
    		</dependency>
    		<dependency>
    			<groupId>org.jsoup</groupId>
    			<artifactId>jsoup</artifactId>
    			<version>1.7.2</version>
    		</dependency>
    		<dependency>
    			<groupId>org.twitter4j</groupId>
    			<artifactId>twitter4j-core</artifactId>
    			<version>4.0.2</version>
    		</dependency>
    	</dependencies>
    </project>
    

    and now I'm getting the following stacktrace.
    java -jar twitter-0.0.1-SNAPSHOT-jar-with-dependencies.jar ryanair.properties
    Failed to load Main-Class manifest attribute from
    twitter-0.0.1-SNAPSHOT-jar-with-dependencies.jar
    

    Here's my maven build stacktrace
    [INFO] Scanning for projects...
    [INFO] 
    [INFO] Using the builder org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder with a thread count of 1
    [INFO]                                                                         
    [INFO] ------------------------------------------------------------------------
    [INFO] Building twitter 0.0.1-SNAPSHOT
    [INFO] ------------------------------------------------------------------------
    [INFO] 
    [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ twitter ---
    [INFO] Deleting /Users/user/Projects/twitter/target
    [INFO] 
    [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ twitter ---
    [INFO] Using 'UTF-8' encoding to copy filtered resources.
    [INFO] Copying 3 resources
    [INFO] 
    [INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ twitter ---
    [INFO] Compiling 5 source files to /Users/user/Projects/twitter/target/classes
    [INFO] 
    [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ twitter ---
    [INFO] Using 'UTF-8' encoding to copy filtered resources.
    [INFO] skip non existing resourceDirectory /Users/user/Projects/twitter/src/test/resources
    [INFO] 
    [INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ twitter ---
    [INFO] Compiling 1 source file to /Users/user/Projects/twitter/target/test-classes
    [INFO] 
    [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ twitter ---
    [INFO] Surefire report directory: /Users/user/Projects/twitter/target/surefire-reports
    
    -------------------------------------------------------
     T E S T S
    -------------------------------------------------------
    Running com.airport.twitter.AppTest
    Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.003 sec
    
    Results :
    
    Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
    
    [INFO] 
    [INFO] --- maven-jar-plugin:2.3.2:jar (default-jar) @ twitter ---
    [INFO] Building jar: /Users/user/Projects/twitter/target/twitter-0.0.1-SNAPSHOT.jar
    [INFO] 
    [INFO] --- maven-assembly-plugin:2.2-beta-5:single (default) @ twitter ---
    [INFO] META-INF/ already added, skipping
    [INFO] META-INF/MANIFEST.MF already added, skipping
    [INFO] META-INF/maven/ already added, skipping
    [INFO] Building jar: /Users/user/Projects/twitter/target/twitter-0.0.1-SNAPSHOT-jar-with-dependencies.jar
    [INFO] META-INF/ already added, skipping
    [INFO] META-INF/MANIFEST.MF already added, skipping
    [INFO] META-INF/maven/ already added, skipping
    [INFO] 
    [INFO] --- maven-install-plugin:2.4:install (default-install) @ twitter ---
    [INFO] Installing /Users/user/Projects/twitter/target/twitter-0.0.1-SNAPSHOT.jar to /Users/user/.m2/repository/com/airport/twitter/0.0.1-SNAPSHOT/twitter-0.0.1-SNAPSHOT.jar
    [INFO] Installing /Users/user/Projects/twitter/pom.xml to /Users/user/.m2/repository/com/airport/twitter/0.0.1-SNAPSHOT/twitter-0.0.1-SNAPSHOT.pom
    [INFO] Installing /Users/user/Projects/twitter/target/twitter-0.0.1-SNAPSHOT-jar-with-dependencies.jar to /Users/user/.m2/repository/com/airport/twitter/0.0.1-SNAPSHOT/twitter-0.0.1-SNAPSHOT-jar-with-dependencies.jar
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 11.220 s
    [INFO] Finished at: 2014-08-26T10:51:18+00:00
    [INFO] Final Memory: 12M/81M
    [INFO] ------------------------------------------------------------------------
    


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 27,370 ✭✭✭✭GreeBo


    <dependency>google</dependency>


  • Registered Users, Registered Users 2 Posts: 6,262 ✭✭✭Buford T Justice


    I've been having trouble following some of the stackoverflow threads that show up when I'm googling.

    From what i can gather, its giving me the error
    Failed to load Main-Class manifest attribute from
    twitter-0.0.1-SNAPSHOT-jar-with-dependencies.jar
    

    because it can't find the Main.java class as an entry point for the program.

    I found earlier that if I added the below to the pom it would solve it. Its in the pom file and still throwing the exception.
    <manifest>
    		<addClasspath>true</addClasspath>
    							             <mainClass>com.airport.twitter.App</mainClass>
    	</manifest>
    

    Edit: I've solved this.

    The jar build with the additional jars built in wasn't told in the pom file to point to the App.java file as an entry point. It needed to have the above code added to the maven-assembly-plugins plugin too. See completed pom file below
    <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>com.airport</groupId>
    	<artifactId>twitter</artifactId>
    	<version>0.0.1-SNAPSHOT</version>
    	<packaging>jar</packaging>
    
    	<name>twitter</name>
    	<url>http://maven.apache.org</url>
    
    	<properties>
    		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    	</properties>
    
    	<build>
    		<plugins>
    			<plugin>
    				<groupId>org.apache.maven.plugins</groupId>
    				<artifactId>maven-jar-plugin</artifactId>
    				<version>2.3.2</version>
    				<configuration>
    					<archive>
    						[B]<manifest>
    							<addClasspath>true</addClasspath>
    							<mainClass>com.airport.twitter.App</mainClass>
    						</manifest>[/B]
    					</archive>
    				</configuration>
    			</plugin>
    			<plugin>
    				<artifactId>maven-assembly-plugin</artifactId>
    				<executions>
    					<execution>
    						<phase>package</phase>
    						<goals>
    							<goal>single</goal>
    						</goals>
    					</execution>
    				</executions>
    				<configuration>
    					<archive>
    						[B]<manifest>
    							<addClasspath>true</addClasspath>
    							<mainClass>com.airport.twitter.App</mainClass>
    						</manifest>[/B]
    					</archive>
    					<descriptorRefs>
    						<descriptorRef>jar-with-dependencies</descriptorRef>
    					</descriptorRefs>
    				</configuration>
    			</plugin>
    		</plugins>
    	</build>
    
    	<dependencies>
    		<dependency>
    			<groupId>junit</groupId>
    			<artifactId>junit</artifactId>
    			<version>3.8.1</version>
    			<scope>test</scope>
    		</dependency>
    		<dependency>
    			<groupId>org.jsoup</groupId>
    			<artifactId>jsoup</artifactId>
    			<version>1.7.2</version>
    		</dependency>
    		<dependency>
    			<groupId>org.twitter4j</groupId>
    			<artifactId>twitter4j-core</artifactId>
    			<version>4.0.2</version>
    		</dependency>
    	</dependencies>
    </project>
    


  • Registered Users, Registered Users 2 Posts: 27,370 ✭✭✭✭GreeBo


    click on my link


  • Registered Users, Registered Users 2 Posts: 6,262 ✭✭✭Buford T Justice


    Thanks for the help boardsies.


Advertisement