I am trying to run my java source code using a jar file which I have created in Ubuntu and I am facing an issue of my jar files not being detected. I have copied all my jar files in a subfolder and placed my sourcecode jar outside. I am trying to call main function from my main jar and I am able to do it successfully but somehow relative jars that are inside my relative subfolders are not detectable. I tried everything including creating or testing a manifest file but it did not work out. My code is running fine in eclipse IDE. Below is my shell script code -
LIB=./ChainLib/
echo "$LIB"
echo ls "$LIB"
CLASSPATH=./ChainLib/*.jar
export CLASSPATH=./ChainLib/*.jar
echo Classpath_is "$CLASSPATH"
#$(JARS=("$LIB"/*.jar); IFS=:;
#echo "${JARS[*]}")
jarnames=`ls ./ChainLib/*.jar`
for eachfile in $jarnames
do
echo $eachfile
done
#java -cp TestBlockchainkaranl.jar Testing
xvfb-run -a java -jar TestBlock.jar
When I run above script I get below:
bash RunBlockchain.sh
./ChainLib/
ls ./ChainLib/
Classpath_is ./ChainLib/*.jar
./ChainLib/bcpkix-jdk14-1.57.jar
./ChainLib/bcprov-jdk15on-1.59.jar
./ChainLib/commons-codec-1.11.jar
./ChainLib/commons-compress-1.5.jar
./ChainLib/fabric-sdk-java-1.2.1.jar
./ChainLib/httpclient-4.1.1.jar
./ChainLib/httpclient-4.5.6.jar
./ChainLib/httpcore-4.4.10.jar
./ChainLib/javax.json-1.1.jar
./ChainLib/netty-common-5.0.0.Alpha2.jar
./ChainLib/org-apache-commons-logging.jar
u1 Username
Y Filestatus
Exception in thread "main" java.lang.NoClassDefFoundError: org/hyperledger/fabric/sdk/User
at Testing.main(Testing.java:26)
Caused by: java.lang.ClassNotFoundException: org.hyperledger.fabric.sdk.User
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
How can I make sure this issue doesn't occur and my jars get detected successfully?
Update: I am adding the manifest file which I had created and zipped it in the jar file
Here is the manifest code:
Manifest-Version: 1.0
Main-Class: Testing
Class-Path:/home/ubuntu/KaranHyperledger/java/Blockchaintool/ChainLib/*.jar
Class-Path:/home/ubuntu/KaranHyperledger/java/Blockchaintool/TestBlock.jar
Java doesn't check CLASSPATH environment variable. You have to pass it explicitly.
java -cp TestBlockchainkaranl.jar:$CLASSPATH Testing
Related
I am trying to get a simple JAR file executing on the CLI (without a manifest). The folder structure is as follows:
mods\un.jar
out\test\Main.class
src\test\Main.java
Here is Main.java:
package test;
public class Main{
public static void main(String []args){
System.out.println("Unnamed module...");
}
}
Here are the commands from the root folder:
javac -d out src\Main.java
java -cp out test.Main - this works
jar -cvf mods\un.jar out\test\Main.class
java -cp mods\un.jar;out test.Main - works (when 'out' is in classpath)
java -cp mods\un.jar test.Main - ERROR
The last line generates the errors:
Error: Could not find or load main class test.Main
Caused by: java.lang.ClassNotFoundException: test.Main
I have looked at the JAR zip and it looks ok. The MANIFEST.MF is the default one but that is okay as I am explicitly telling Java what class to start with (the class with main()). The contents are:
META-INF\MANIFEST.MF
out\test\Main.class
The manifest itself is:
Manifest-Version: 1.0
Created-By: 11 (Oracle Corporation)
So it looks like the class file in the JAR is not being picked up at all. If I omit the out folder when I run java or if I rename the .class file in the out folder), I get the class not found error. I would like to get it working without a manifest.
Any ideas?
Thanks,
Seán.
You need to change to the 'out' directory to get your package root correct in the jar:
jar -cvf mods\un.jar -C out test\Main.class
I am trying to generate java files from asn1 file using the below in my android project and facing error in generating the files in Mac OS and in window it worked fine.
org.openmuc:jasn1:1.9.0
org.openmuc:jasn1-compiler:1.9.0
Below is the bash file
#!/bin/bash
JARS_LOCATION="../build/libs-all"
MAIN_CLASS="org.openmuc.jasn1.compiler.Compiler"
SYSPROPS=""
PARAMS=""
SCRIPT_HOME=`dirname $0`
CLASSPATH=$(JARS=("$SCRIPT_HOME"/"$JARS_LOCATION"/*.jar); IFS=:; echo "${JARS[*]}")
for i in $#; do
if [[ $i == -D* ]]; then
SYSPROPS="$SYSPROPS $i";
else
PARAMS="$PARAMS $i";
fi
done
java $SYSPROPS -cp $CLASSPATH $MAIN_CLASS $PARAMS
and the above bash file will be called from my projects build.gradle file.
Below is the final command will be executed from the bash. Shortened the file paths for easy understanding.
java -cp "/libs/antlr-2.7.7.jar:/libs/jasn1-1.9.0.jar:/libs/jasn1-compiler-1.9.0.jar" org.openmuc.jasn1.compiler.Compiler -p com.test.package.asn1 -f /asn1def/asn1/RSPDefinition.asn /asn1def/asn1/PKIX1Explicit88.asn /asn1def/asn1/PKIX1Implicit88.asn -o /asn1def/build/generated/source/java
Below is the error I am getting
Generated code will be saved in /asn1def/build/generated/source/java
Parsing "/asn1def/asn1/RSPDefinition.asn"
Parsing "/asn1def/asn1/PKIX1Explicit88.asn"
Parsing "/asn1def/asn1/PKIX1Implicit88.asn"
Generating classes for module "PKIX1Implicit88"
Exception in thread "main" java.lang.NoClassDefFoundError: javax/xml/bind/DatatypeConverter
at org.openmuc.jasn1.compiler.HexConverter.toShortHexString(HexConverter.java:63)
at org.openmuc.jasn1.compiler.HexConverter.toShortHexString(HexConverter.java:59)
at org.openmuc.jasn1.compiler.HexConverter.appendShortHexString(HexConverter.java:99)
at org.openmuc.jasn1.compiler.HexConverter.appendHexString(HexConverter.java:108)
at org.openmuc.jasn1.compiler.HexConverter.toHexString(HexConverter.java:9)
at org.openmuc.jasn1.compiler.BerClassWriter.writeEncodeTag(BerClassWriter.java:2169)
at org.openmuc.jasn1.compiler.BerClassWriter.writeSequenceOrSetEncodeFunction(BerClassWriter.java:1107)
at org.openmuc.jasn1.compiler.BerClassWriter.writeSequenceOrSetClass(BerClassWriter.java:731)
at org.openmuc.jasn1.compiler.BerClassWriter.writeConstructedTypeClass(BerClassWriter.java:493)
at org.openmuc.jasn1.compiler.BerClassWriter.translateModule(BerClassWriter.java:194)
at org.openmuc.jasn1.compiler.BerClassWriter.translate(BerClassWriter.java:133)
at org.openmuc.jasn1.compiler.Compiler.main(Compiler.java:89)
Caused by: java.lang.ClassNotFoundException: javax.xml.bind.DatatypeConverter
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 12 more
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':asn1def:compileAsn'.
> Process 'command '/asn1def/run-scripts/jasn1-compiler'' finished with non-zero exit value 1
I have the main class in the jasn1-compiler-1.9.0.jar file in the libs folder. The same generate files in windows but in Mac it is not working. The difference in windows & Mac is Java version. Mac has Java 11 & windows has Java 1.8. I also tried adding the JDK 1.8 location as below at the end of above code.
-Dorg.gradle.java.home=/Users/Shared/Jenkins/.jenkins/tools/hudson.model.JDK/JDK_1_8
Can anyone help me to point out what's missing?
Finally I found the answer. The error was happened because of the Java version in Mac, it has Java 11 and it didn't support javax xml bind. So I installed the JDK 1.8 in the mac machine and use that path in the bash file as below before the java execution line in bash file.
export JAVA_HOME=/usr/java/jdk1.8.161/
It may useful for someone.
I have a jar file named- ParseCSV-0.0.1-SNAPSHOT.jar and the contents of it are -
META-INF/
META-INF/MANIFEST.MF
foo/
foo/ReadCSV.class
META-INF/maven/
META-INF/maven/Test/
META-INF/maven/Test/ParseCSV/
META-INF/maven/Test/ParseCSV/pom.xml
META-INF/maven/Test/ParseCSV/pom.properties
The main class is ReadCSV.class and it takes a CSV file (C:\Testting\ValidaResults.csv) as an input parameter. I am trying to execute the jar file from the command promt by running the following command-
java -jar . ParseCSV-0.0.1-SNAPSHOT.jar "C:\Testting\ValidationResult.csv"
But I am getting an error saying-
Error: Could not find or load main class foo.ReadCSVTest
What is going wrong here.
Why do you have a . between jar and ParseCSV..?
Try this first:
java -jar ParseCSV-0.0.1-SNAPSHOT.jar "C:\Testting\ValidationResult.csv"
If this does not work, then try:
java -cp ParseCSV-0.0.1-SNAPSHOT.jar;. foo.ReadCSV "c:\Testting\ValidationResult.csv"
If this works, then i would inspect the META-INF/MANIFEST.MF file to figure out what the classpath is like, and what the value of Main-Class is. It seems to be running ReadCSVTest instead of ReadCSV.
Your JAR file includes foo/ReadCSV.class, but your MainClass is set to foo.ReadCSVTest. You'll need to fix your Main-Class attribute in META-INF/MAINIFEST.MF
I've a java program as shown below:
package com.abc.myproject;
import java.util.*;
class test {
public static void main(String[] args)
{
ResourceBundle rb = ResourceBundle.getBundle("mybundle");
String propertyValue = rb.getString("name");
System.out.println("propertyValue="+propertyValue);
}
}
I've this program located under C:\testing\code\src\com\abc\myproject\test.java
And, I've kept mybundle.properties under C:\testing\code folder.
mybundle.properties contain one line:
name=Mike
When I run the program from command prompt as shown below, it runs perfectly fine:
C:\testing\code\src>java -cp .;c:\testing\code com.abc.myproject.test
propertyValue=Mike
Now, I created Jar file xyz.jar containing only test.class & kept this Jar in C:\testing directory & the manifest of jar contains below:
Manifest-Version: 1.0
Created-By: 1.7.0_05 (Oracle Corporation)
Main-Class: com.abc.myproject.test
In the same directory C:\testing, I kept mybundle.properties & then tried to run jar with below commands:
java -cp .;c:\testing -jar xyz.jar
But it fails to load mybundle resource & throws error:
Exception in thread "main" java.util.MissingResourceException: Can't find bundle
for base name scheduler, locale en_US
at java.util.ResourceBundle.throwMissingResourceException(Unknown Source)
at java.util.ResourceBundle.getBundleImpl(Unknown Source)
at java.util.ResourceBundle.getBundle(Unknown Source)
at com.abc.myproject.test.main(test.java:8)
I have to keep the mybundle.properties out side of Jar since we should be able to make changes in this file without having to redeploy the Jar.
Can any one please help me in fixing this issue?
Thanks!
java -cp .;c:\testing -jar xyz.jar
You cannot add to the classpath when using -jar. $CLASSPATH and -cp are ignored, and it will only use the classpath in the jar manifest.
What you can do is run your class like this:
java -cp .;c:\testing;xyz.jar com.abc.myproject.test
I am trying to run simple java code on VMWare Workstation. I have the following simple test Main file:
import cern.jet.random.engine.RandomSeedGenerator;;
public class TestDataService {
//private static Logger logger = Logger.getLogger(TestDataService.class);
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Hello World DAI!");
// Input some data.
RandomSeedGenerator re = new RandomSeedGenerator();
return;
}
}
RandomSeedGenerator is a class in colt.jar library, and I have the jar file under my lib folder.
I am building the project with ant, and I have the following manifest file where I set the classpath:
Manifest-Version: 1.0
Main-Class: edu.umass.TestDataService
Name: edu/umass/TestDataService/Version.class
Class-Path: lib/colt.jar
When I run the code from the VMWare shell which runs Red Hat Linux, I get this Exception:
[root#localhost] java -jar app.jar
Hello World DAI!
Exception in thread "main" java.lang.NoClassDefFoundError: cern/jet/random/engine/RandomSeedGenerator
at edu.umass.TestDataService.main (Unknown Source)
Caused by: java.long.ClassNotFoundException: cern.jet.random.engine.RandomSeedGenerator
Just as a final note, everything seems to work fine on windows with eclipse, but nothing seems to work on the virtual machine. Any ideas?
Did you install the jar files required by your application on the VMs?
Did you configured CLASS_PATH correctly?
I doubt there is an issue with the jvm or the vm. The problem is going to be in how you run the class. Specifically how your setting the classpath. Try this:
Navigate to where you've placed colt.jar. Get the present working directory by typing in pwd. Use this to construct the run command using the absolute path to colt.jar.
So eventually you should be running (from the directory containing your jar) something like this:
java -cp /the/full/path/to/lib/colt.jar -jar app.jar
Once you've got that work you can then try and figure out what the correct relative path is. and then you'll be able to do
java -cp a/relativel/path/to/lib/colt.jar -jar app.jar