Classpath in Manifest - java

I have download a xSocket.jar which will be use as a classpath and compile myprogram.jar, both are in Java folder.
Is adding a classpath in Manifest able to find xSocket.jar without the need to define a -cp in commandline?
In my commandline D:\> location, I tried running java -jar java\myprogram.jar -n 0
Exception in thread "main" java.lang.NoClassDefFoundError: org/xsocket/connectio
n/IBlockingConnection
at myprogram.main(myprogram.java:114)
Caused by: java.lang.ClassNotFoundException: org.xsocket.connection.IBlockingCon
nection
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:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
... 1 more
My Manifest in myprogram.jar:
Manifest-Version: 1.0
Created-By: 1.6.0_22 (Sun Microsystems Inc.)
Main-Class: myprogram
Class-Path: xSocket

You need to specify the full jar file name i.e. with extension. Check here for more information.
Class-Path: xSocket.jar

Is adding a classpath in Manifest able
to find xSocket.jar without the need
to define a -cp in commandline?
Yes, it's the way.
You should change your class pass value in myprogram.jar to
Class-Path: relatedPath/xSocket.jar

Related

what is the alternative for Java Jar command to run a jar with scala class [duplicate]

I got some problems with the java. Check it out.
sebastian#sebastian-desktop:~/scaaaaaaaaala$ java -cp /home/sebastian/.m2/repository/org/scala-lang/scala-library/2.8.0.RC3/scala-library-2.8.0.RC3.jar:target/scaaaaaaaaala-1.0.jar scaaalaaa.App
Hello World!
That's cool, right, but how bout this:
sebastian#sebastian-desktop:~/scaaaaaaaaala$ java -cp /home/sebastian/.m2/repository/org/scala-lang/scala-library/2.8.0.RC3/scala-library-2.8.0.RC3.jar -jar target/scaaaaaaaaala-1.0.jar
Exception in thread "main" java.lang.NoClassDefFoundError: scala/Application
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)
at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
at scaaalaaa.App.main(App.scala)
Caused by: java.lang.ClassNotFoundException: scala.Application
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:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
... 13 more
Wat the heck? Any idea why the first works and not the 2nd? How do I -jar my scala??
My thanks in advance, brother.
If you define -jar the -classpath is ignored:
Java manual:
-jar When you use this option, the JAR file is the source of all user
classes, and other user class path
settings are ignored.
You can define the classpath dependencies in the Manifest metadata.
The easiest way to start your app is using the scala scripts:
scala -classpath target/scaaaaaaaaala-1.0.jar scaaalaaa.App Hello World!
Simply running
scala scaaaaaaaaala-1.0.jar
works for me with 2.11.6
use sbt-assembly sbt plugin to produce a jar containing all dependencies
sbt-assembly github
e.g. add a line in project/plugins.sbt:
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.10")
adjust your build.sbt e.g. with
mainClass in (assembly) := Some("mypackage.mysubpackage.MyMainClass")
assemblyJarName in assembly := "my_assembly.jar"
then build your assembled jar with
sbt assembly
and launch it with
java -jar ./target/scala-2.12/my_assembly.jar
Et voilĂ , no class-path this & thats needed any more. only 1 jar.
For an executable jar, the classpath should be in the jar's manifest. For help on doing that, look through the answers to Stackoverflow: How to create an executable jar with dependancy jars.
Below is the way to execute the Scala Executable Jar
We need scala installed in your system.
Here first will give the jar path and jar name
If your code needs any dependency jar then keep all jar in one directory and give a path of this in command like below after the ":".
At last give your class/object name.
scala -cp "/your/jar/path/jar_name.jar:/your/dependency/jar/path/*"
SampleCode

Java file runs but not Jar file

I'm trying to create a jar file for one of my files (Worker.java). The file is in the beatDB package in the src/ folder.
To create the jar I ran jar cfm InitializeInstanceWorker.jar Manifest.txt -C src/ . and my Manifest.txt contains: Main-Class: Main-Class: beatDB.Worker \n with a new line at the end.
When I call java beatDB/Worker the file runs, but then when I try to run the jar file, java.lang.ClassDefNotFoundException:
Exception in thread "main" java.lang.NoClassDefFoundError: com/amazonaws/auth/AWSCredentialsProvider
Caused by: java.lang.ClassNotFoundException: com.amazonaws.auth.AWSCredentialsProvider
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)
I have aws-java-sdk-1.7.7.jar in my lib folder, which is on the classpath, which I set in my .bash_profile file.
Does anyone know why running the java file directly works, but the jar file does not run?
The ways that you can run your jar/main class are:
java -cp InitializeInstanceWorker.jar;lib\aws-java-sdk-1.7.7.jar beatDB.Worker
Or
java -cp lib\aws-java-sdk-1.7.7.jar beatDB.Worker -jar InitializeInstanceWorker.jar`
Or, if you add the attribute Class-Path: lib/ in your jar, you will be able to run it by:
java -jar InitializeInstanceWorker.jar
Hope it helps!

How to set Lucene with following error on Mac?

I am having trouble on setting up classpath on Mac. The following is the code I have attempted,
First I exported the jars using terminal
abc-MacBook-Pro:~ abc$ export
CLASSPATH=$CLASSPATH:/Users/abc/Desktop/IR/luceneJar/demo/lucene-demo-4.0.0.jar: export
CLASSPATH=$CLASSPATH:/Users/abc/Desktop/IR/luceneJar/core/lucene-core-4.0.0.jar: export
CLASSPATH=$CLASSPATH:/Users/abc/Desktop/IR/luceneJar/queryparser/lucene-queryparser-4.0.0.jar: export
CLASSPATH=$CLASSPATH:/Users/abc/Desktop/IR/luceneJar/analysis/common/lucene-analyzers-common-4.0.0.jar
Then when I echoed the classpath, following is the result
abc-MacBook-Pro:~ abc$ echo $CLASSPATH
/Users/abc/Desktop/IR/lucene-4.0.0 2/core/lucene-core 4.0.0.jar:
/Users/abc/Desktop/IR/lucene-4.0.0 2/demo/lucene-demo-4.0.0.jar:
/Users/abc/Desktop/IR/luceneJar/analysis/common/lucene-analyzers-common-4.0.0.jar
The error I am having is following when I tried to function the index file
abc-MacBook-Pro:~ abc$ java org.apache.lucene.demo.IndexFiles -docs
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/lucene/demo/IndexFiles
Caused by: java.lang.ClassNotFoundException: org.apache.lucene.demo.IndexFiles
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)
Please let me know about the above error, thank you in advance. I am quite new with Lucene Java.
This is common Java classpath problem, nothing related to lucene (or mac) here.
Try this
1) Create lib folder and move all your lucene jars to lib
- lets say your project has two folders now lib and src, lib has all jars and src has yourJavaFile.java
2) Run below commands for compilation and execution from your src
- javac -cp ".:../lib/*" yourJavaFile.java
- java -cp ".:../lib/*" yourJavaFile

Could not find the main class

I am trying to run a java program bundled in the jar named easyflow-gui.jar using cmd:
java -classpath "." -jar easyflow-gui.jar
The working directory is the directory containing all relevant libraries.
The content of the Manifest file of the jar file I am trying to run is:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.3
Created-By: 1.6.0_32-b27 (Sun Microsystems Inc.)
Main-Class: easyflow.custom.jgraphx.editor.SchemaEditor
The result of this attempt is:
Exception in thread "main" java.lang.NoClassDefFoundError: com/mxgraph/util/mxEventSource$mxIEventListener
Caused by: java.lang.ClassNotFoundException: com.mxgraph.util.mxEventSource$mxIEventListener
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: easyflow.custom.jgraphx.editor.SchemaEditor. Program will exit.
Question: which one of the classes is actually not found: the mxEventSource$mxIEventListener or the main class easyflow.custom.jgraphx.editor.SchemaEditor ?
Edit 1:
I checked the folder and extracted the jars and I find both classes available (bundled into its respective jars in the working dir):
$ls easyflow/custom/jgraphx/editor/SchemaEditor*
easyflow/custom/jgraphx/editor/SchemaEditor$1.class
easyflow/custom/jgraphx/editor/SchemaEditor$2.class
easyflow/custom/jgraphx/editor/SchemaEditor.class
easyflow/custom/jgraphx/editor/SchemaEditor.java
$ls com/mxgraph/util/mxEventSource*
com/mxgraph/util/mxEventSource$mxIEventListener.class
com/mxgraph/util/mxEventSource.class
You cannot specify "." for the classpath if you want to include a jar file, it has to be a colon-separated list of jar files or directories (or semicolon-separated, depending on the operating system). Try java -help to get a description of the command line options.
Also if I remember correctly -jar and -classpath do not work together, so you have to use -classpath alone and specify the main class explicitly.
Try something like
java -cp easyflow-gui.jar:foo.jar:bar.jar easyflow.custom.jgraphx.editor.SchemaEditor
... where foo.jar and bar.jar are the "other relevant libraries".
If you read the exception properly, you will see that the cause of the error:
Caused by: java.lang.ClassNotFoundException: com.mxgraph.util.mxEventSource$mxIEventListener
You didn't include the jar file that contains the com.mxgraph.util.mxEventSource$mxIEventListener in your classpath. From a quick Google search, you will need jGraph library.
I hope this helps.
The default classpath is the current working directory.
So, if you already have the jar in your current working directory, you dont need to specify the classpath explicitly.
The following command should work
java -jar easyflow-gui.jar

problem related to MANIFEST.MF in jar

I have created jar in folder /usr/local/bin/niidle.jar.
And I have cerated MANIFEST.MF in jar as follows:
Manifest-Version: 1.0
Main-Class: com.ensarm.niidle.web.scraper.NiidleScrapeManager
Class-Path: hector-0.6.0-17.jar
And my 'hector-0.6.0-17.jar' file is present in following folder:
/Projects/EnwelibDatedOct13/Niidle/lib/hector-0.6.0-17.jar
And I have added this 'hector-0.6.0-17.jar' in niidle.jar folder as follows:
niidle.jar/lib/hector-0.6.0-17.jar
And when I run this using command:
java -jar /usr/local/bin/niidle.jar arguments....
then I am getting error message as follows:
Exception in thread "main" java.lang.NoClassDefFoundError: me/prettyprint/hector/api/Serializer
at com.ensarm.niidle.web.scraper.NiidleScrapeManager.main(NiidleScrapeManager.java:21)
Caused by: java.lang.ClassNotFoundException: me.prettyprint.hector.api.Serializer
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
... 1 more
And When I run again using different command as follows:
java -cp /Projects/EnwelibDatedOct13/Niidle/lib/hector-0.6.0-17.jar -jar /usr/local/bin/niidle.jar arguments.....
---------------Then also I am getting the same error message:--
Exception in thread "main" java.lang.NoClassDefFoundError: me/prettyprint/hector/api/Serializer
at com.ensarm.niidle.web.scraper.NiidleScrapeManager.main(NiidleScrapeManager.java:21)
Caused by: java.lang.ClassNotFoundException: me.prettyprint.hector.api.Serializer
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
... 1 more-------------
so tell me what is solution for that.or any modification in MANIFEST.MF in jar
java -cp /Projects/EnwelibDatedOct13/Niidle/lib/hector-0.6.0-17.jar -jar /usr/local/bin/niidle.jar arguments.....
You can't use the -cp switch together with the -jar switch.
From http://mindprod.com/jgloss/classpath.html:
If you use the -jar option, java.exe ignores the classpath. It will only look in that jar!! Try using the manifest Class-Path instead to get Java to look in auxiliary jars
And I have added this 'hector-0.6.0-17.jar' in niidle.jar folder as follows:
To my knowledge, you can't nest .jar files this way.
Since you have Class-Path: hector-0.6.0-17.jar you need to have the needle.jar and the hector-0.6.0-17.jar side by side and run niidle.jar from their common directory (using the -jar switch if you like).
Line niidle.jar/lib/hector-0.6.0-17.jar is confusing. Is niidle.jar directory or file?
I'd suggest you to post your full manifest, directory structure, command line you are using and current working directory.
But generally you should write in your manifest:
Class-Path: hector-0.6.0-17.jar
then put all jars in one directory. Let's call it foo.
Now open command prompt, go to foo and run
java -jar niidle.jar args...
And I believe that everything will work. Then if you wish play with location of your jars.
For setting up "jars within jars" take a look at this question:
Classpath including JAR within a JAR

Categories

Resources