Ini4j classpath no class - java

i am want create jar of my project using cmd line.
I am using ini4j libs.
Everything compiles fine, but I do not know how to set the -cp to the library.
Compile:
javac -cp ".;lib/ini4j-0.5.2.jar;ini4j-0.5.2-jdk14.jar;lib/ini4j-0.5.2-jdk14.jar" gui_Frame/*.java
Create jar:
echo Main-class: gui_Frame/MainApp > manifest.txt
jar cvfm GVE.jar manifest.txt gui_Frame/*
But, if i want start java -jar GVE.jar i get following error:
Java.lang.NoClassDefFoundError: org/ini4j/wini
What am I doing wrong ?

You must specify the same classpath when running java as you did when compiling.
Or bundle all the necessary class files in one JAR.
==EDIT==
Try this:
java -cp "GVE.jar;.;lib/ini4j-0.5.2.jar" gui_Frame.MainApp

Related

how to use remote jars in classpath for javax.tools.JavaCompiler

I'm using JavaCompiler to compile .java code.
It works for me while running the class compiled using URLClassLoader to load remote jars.
But not work while compiling.
Tried to use options like
options.addAll(Arrays.asList("-classpath", "https://example.com/example.jar"));
Also tried to use customized JavaFileManager with URLClassLoader.
None of them works.
try below commands, and replace the .jar file with your external .jar file... into the command prompt or terminal.
$ javac -cp ".:./jars/common.jar" helloworld.java
$ java -cp ".:./jars/common.jar" helloworld

How to include dependency jar files into jar file

I'm build a jar file with code that uses other jar files as dependencies, and I can't get it to run (I keep getting a NoClassDefFoundError).
All the other similar issues I could find online used maven as a solution, which I want to avoid doing. When I run my code like this:
#!/bin/bash
java -cp "src/.:src/jsoup-1.9.2.jar:src/commons-cli-1.3.1.jar" WikipediaWords $#
it runs fine, but when I try:
java -cp "src/.:src/jsoup-1.9.2.jar:src/commons-cli-1.3.1.jar" -jar WikipediaWords.jar $#
it complains.
I compile my jar file with all the dependencies inside, though, so I really have no clue why it can't find anything. Ideally, I wouldn't even want to have to add the -cp path to my java execution, and just have the jar file know to use the nested jar dependency files.
If you have any clue about how to solve this (run the jar file without class def error) without the use of Maven, please let me know!
Thanks in advance!
P.S. My building code might be of use:
#!/bin/bash
javac -cp "src/.:src/jsoup-1.9.2.jar:src/commons-cli-1.3.1.jar" src/WikipediaWords.java
jar cfm src/WikipediaWords.jar src/Manifest.txt -C src/ .
Try running the below command:
java -cp "src/.:src/jsoup-1.9.2.jar:src/commons-cli-1.3.1.jar:WikipediaWords.jar" WikipediaWords $#
OR
java -cp "WikipediaWords.jar:src/.:src/jsoup-1.9.2.jar:src/commons-cli-1.3.1.jar" WikipediaWords $#

Run jar file from unix command prompt (ERROR)

I have a java project that has 10 .java files. A1.java, A2.java......A9.java and Main.java.
The project contains a package grand.big.medium.small and all the .java files are inside this package.
I have compiled and created an executable jar file for the project using:
javac grand/big/medium/small/*.java
to run the project I used the command
java -cp . grand/big/medium/small/Main
to create the jar I used the command
jar -cvmf manifest.txt MyJAR.jar *.class
I want to run the jar file from command prompt and I am using the commad
java -cp . -jar grand/big/medium/small/MyJAR.jar
and I getting an error:
Error: Could not find or load main class Main
please can anyone tell me why I am getting this error?
Run as:
java -cp . -jar grand/big/medium/small/MyJAR.jar grand.big.medium.small.Main
Assuming you have grand as a directory within current directory and directory hierarchy follows.

Run a jar using a shell file and specify classpath and properties file path

I've compiled a Spring Maven project JAR. My dependencies are all in a directory labeled lib. How to execute my JAR while specifying it should use the LIB/* as the classpath and also use log4j.xml and a properties file called marker.properties. My marker.sh
LIB=$INSTALL_PATH/lib
echo "$LIB" # outputs usr/local/app/lib
export CLASSPATH=$(JARS=("$LIB"/*.jar); IFS=:; echo "${JARS[*]}")
PARM="-Dlog4j.configuration=/usr/local/Marker/log4j.xml"
java $PARM -jar /usr/local/Marker/marker-1.0.jar
But on running this I get the following error as it is not able to find the jars in the classpath
Exception in thread "main"java.lang.NoClassDefFoundError: org/springframework/context/ConfigurableApplicationContext at java.lang.Class.getDeclaredMethods0(Native method)
However, this command is able to load the jar from classpath, but I am unable to add the properties file
java -cp /usr/local/Marker/marker-1.0.jar:"$LIB"/* com.package.Marker
java -cp /usr/local/Marker/marker-1.0.jar:"$LIB"/* com.package.Marker worked perfectly fine. I needed to add .properties file and log4j.xml in folder where jar was placed
You can specify the classpath using the -classpath (or -cp) flag; for example:
~$ java -jar package.jar -classpath /path/to/classpath/*
For more information, see the following documentation.

Running jar from different directory cannot find required dependency

I'm trying to run a jar ec/mobat/MOBAT.jar which depends on some jars located in ec/mobat/lib/. It works if I do:
ec/mobat/$ java -jar MOBAT.jar
However I want to be able to run the jar from another directory
ec/$ java -jar mobat/MOBAT.jar
But I get an exception
java.lang.NoClassDefFoundError: ibis/io/Serializable
...
I tried to pass the required jars in the classpath
ec/$ CLASSPATH=... java -jar mobat/MOBAT.jar
ec/$ java -jar -cp ... mobat/MOBAT.jar
but I get exactly the same exception. Any fix?
Update: MANIFEST.INF contains the following:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.7.0
Created-By: Selmar Kagiso Smit
Main-Class: mobat.Launcher
Implementation-Version: 1.3.4
The classpath has to contain every jar you're depending on.
java -classpath b.jar;c.jar -jar a.jar //does not work see below
The ";" is system dependent for windows ":" for unix.
The jar switch is used to select the jar file whose main class is executed (Main-Class: mobat.Launcher in the manifest file). The command line:
java -classpath b.jar;c.jar;a.jar mobat.Launcher
Would produce the same result.
Alternatively classpath definitions can be added to the Manifest file. Your manifest file could contain the attribute.
Class-Path: lib/b.jar lib/c.jar
Then
java -jar a.jar
would work.
Edit:
I thought that -jar and -cp could be used together. But the java tools documentation is clear:
-jar
When you use this option, the JAR file is the source of all user
classes, and other user class path
settings are ignored.
Only the manifest and everything explict (classpath and main class) versions work.
You can not use -cp and -jar together
java -cp myjar.jar;lib/*;. mypackage.MyClass
shall work on windows and
java -cp myjar.jar:lib/*:. mypackage.MyClass
shall work on Unix

Categories

Resources