Error in executing a jar file - java

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

Related

class file in JAR seems to be ignored; class file being picked up from directory on classpath instead

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

Issue with the java classpath in shell script - jars not detectable

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

Command: java -jar [...] Fails with Error Message

I am running java 1.8.0_65 on Windows 7.
I create a JAR and run it with the following command:
java -jar printxml.jar
And get this error:
Error: Could not find or load main class printxml.PrintXml
Here is my command to create the JAR:
jar cmfev manifest.txt printxml.jar printxml.PrintXml #filelist.txt
Contents of file "manifest.txt":
Class-Path: C:\Users\Me\SQLSER~1\JDBC\jtds-1.3.1.jar
I checked whether printxml.PrintXml class is in the JAR via this command:
jar tvf printxml.jar printxml/PrintXml.class
The command succeeded, i.e. PrintXml class is in the JAR.
I then checked if the PrintXml class in the JAR has a "main" method via this command:
javap -classpath printxml.jar -public printxml.PrintXml
The command succeeded and its output included...
public static void main(java.lang.String[]);
Searching the Internet, I found only the obvious answers, like:
Your classpath is wrong.
Your class doesn't have a "main" method.
Can someone please tell me how to resolve this problem?
Thanks,
Avi.
As Homer Simpson would say: D'OH
The value of the Class-Path entry in file "manifest.txt" is wrong!
It needs to be a URL!
So I changed it to:
file:/C:/Users/Me/SQLSER~1/JDBC/jtds-1.3.1.jar
Hey presto! No more error message. Now it runs!
Thanks to all who helped. ;-)

What's wrong in my classpath configuration with java 1.7?

I'm trying to run a complied class with the command:java SocketTest in current directory.
But something wrong:
Exception in thread "main"
java.lang.NoClassDefFoundError:SocketTest(wrong
name:socket/SocketTest)...
here is my classpath AND path configration in windows XP:
JAVA_HOME:
C:\Program Files\Java\jdk1.7.0_25\
classpath:
.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar
path:
%JAVA_HOME%\jre\bin;C:\Program Files\Java\jdk1.7.0_25\bin;C:\Ruby187\bin;H:\Program Files\ARM\ADSv1_2\bin;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%JAVA_HOME%\bin;C:\Program Files\Microsoft SQL Server\80\Tools\BINN
I think the .; has been carefully written,so I'm not sure what's wrong with the java command. Someone help,please!
The error message has pointed out you need to put the your class to the corrected package socket. So you need to create folder with name socket and then put your SocketTest.class to this folder and run the following command (under the parent folder of socket):
java socket.SocketTest

Unable to locate resource outside of Jar

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

Categories

Resources