Naresh
Naresh

Reputation: 5397

Issues while executing jar using command line in ubuntu

I have one DataMerge java file in com.test.data package which import following packages :

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

Now when i created jar(say Merge.jar) file of DataMerge.java. It only contains the DataMerge.class file no other dependent jars. So my first question is can we somehow include the dependent files in the Merge.jar? Now i run this Merge.jar on ubuntu using the following command :

java -cp /home/user/Desktop/test/*.jar -jar Merge.jar

Which is not a valid command which i came to realize after reading on stackoverflow and it gives the error of Class not found.

Now i tried using the following command

java -cp ./*.jar Merge.DataMerge  (Merge is the jar file name and DataMerge is the main class)

which again throw the following exception. Which i guess because of dot(.) in the name of jar (commons-lang-2.5.jar) file.

 Exception in thread "main" java.lang.NoClassDefFoundError: //commons-lang-2/5/jar
Caused by: java.lang.ClassNotFoundException: ..commons-lang-2.5.jar
    at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: ./commons-lang-2.5.jar. Program will exit

So, how do i execute my jar which is made on windows using eclipse on ubuntu from command line having jar dependency having names like commons-lang-2.5.jar, slf4j-api-1.5.5.jar, commons-logging-1.1.1.jar?

Upvotes: 1

Views: 598

Answers (2)

piet.t
piet.t

Reputation: 11911

When using -cp ./*.jar you have to make sure the wildcard isn't expanded by the shell before being passed to java - so you have to quote the classpath like -cp "./*.jar". Then everything should be working provided all needed jars are in the current directory.

Upvotes: 1

Aman Gautam
Aman Gautam

Reputation: 3579

Two things you have to care about, assuming you are doing an File -> Export -> Java -> Runnable JAR file.

  1. Launch Configuration: This should point to the class you want to start the code execution with. It should have a public static void main(String s[]) function defined.
  2. Library Handling: In your case you want to choose "Package required libraries into generated JAR".

Then you can just do java -jar <your_jar>

Upvotes: 1

Related Questions