0

i am working on a project that uses a jnlp file. in there there are resources specified like this:

[code]

<resources>
    <jar href="noterik-apu.jar" main="true" download="eager"/>
    <jar href="springfield-tools.jar" download="eager"/>
    <jar href="commons-httpclient-3.1.jar" />
    <jar href="commons-cli-1.2.jar" />
    <jar href="org.apache.commons.codec.jar" />
    <jar href="org.apache.commons.httpclient.jar" />
    <jar href="org.apache.commons.logging.jar" />
    <jar href="swing-worker-1.1.jar" />
    <jar href="log4j-1.2.16.jar" />
    <jar href="dom4j-1.6.1.jar" />
    <jar href="jaxen-1.1.1.jar" />
    <j2se version="1.6+" href="http://java.sun.com/products/autodl/j2se"/>
</resources>

[/code]

the ant build file does not include those jar files in the build .jar. how can i add those jars in the cli so that it doesn't give me "Exception in thread "main" java.lang.NoClassDefFoundError:" ? i have tried "-cp file1.jar:file2.jar:etc.jar" but i read on the internet that you can't use -cp with -jar.. how can i make it work?

thanks in advance!

tk66
  • 274
  • 7
  • 20
  • *"uses a jnlp file"* Where? How? ..What? A JNLP is used for launching desktop apps. from a link on a network. It has no close connection to a command line app. – Andrew Thompson May 31 '12 at 09:32
  • Drop the jars in the project's lib folder. – Kumar Vivek Mitra May 31 '12 at 09:36
  • i already have them in the lib dir. when i export the project from eclipse i can just run it with java - jar ... but i am working with jenkins, and it builds the project using the ant build file. The build.xml doesn't add the jars in the final jar. – tk66 May 31 '12 at 09:48
  • the jnlp file is used to start the application for the clients. what i want is to have a script on the server that takes the latest build of jenkins, and run the application without using the jnlp.. so i have to provide those jars as parameters to the terminal – tk66 May 31 '12 at 09:49

2 Answers2

1

You shouldn't embed jar files inside another jar file. That can only work if you use a special classloader. Use the -cp option when starting your app:

java -cp lib\*:.\myApp.jar com.foo.bar.MyApp

Or include the relative paths of the jar files into the manifest of myApp.jar by following the instructions on this page, and start your app with java -jar myApp.jar.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • i don't want to embed the jar files in the build jar. but the application.jar that i have, i want to run it from the command line and give it as arguments the jar libraries instead of depending on jnlp to specify them. but from what i understand i cant run "java -cp lib1.jar:lib2.jar -jar application.jar . – tk66 May 31 '12 at 10:59
  • So what. Read my answer. It explains how you must do. – JB Nizet May 31 '12 at 11:00
  • java -cp springfield-tools.jar:commons-cli-1.2.jar:commons-httpclient-3.1.jar:dom4j-1.6.1.jar:jaxen-1.1.1.jar:log4j-1.2.16.jar:mail.jar:mailapi.jar:org.apache.commons.codec.jar:org.apache.commons.httpclient.jar:org.apache.commons.logging.jar:smtp.jar:springfield-tools.jar:swing-worker-1.1.jar noterik-apu.jar does this look right to you? – tk66 May 31 '12 at 11:05
  • Does it look like what I said in my answer? No. Your app jar must be in the classpath, with all the other jars, and you must pass the main class name as argument. Why do you ask for answers if you don't read them? – JB Nizet May 31 '12 at 11:07
  • sorry.. i am kind of a noob when it comes to this things..! i am working on my first big project! sorry for my ignorance! – tk66 May 31 '12 at 11:13
  • 1
    Your ignorance is fine. Everybody had to learn. But if you ask a question, listen to answers. That has nothing to do with ignorance. – JB Nizet May 31 '12 at 11:16
0

If you are on linux while compiling use

$ javac -cp myjar.jar Hello.java

And while running use, need to include current directory with dot

$ java -cp myjar.jar:. Hello
Akhilesh
  • 1,400
  • 3
  • 15
  • 20