jRuby gem in a jar generates directory instead of jar - java

I'm using jruby and have been putting my gems in a jar like this
java -jar jruby_complete.jar -S gem install -i gem_jar.jar nokogiri erubis
This creates a jar "file" and correctly installs the gems. I can add this jar "file" to my search path and my project will run, however when I go to do a distribution build in netbeans, it complains about the gem_jar being a directory.
A .jar is just a zipped directory, or so I thought, so I ran
zip gem_jar.zip gem_jar; mv gem_jar.jar gem_jar.old; mv gem_jar.zip gem_jar.jar
Netbeans no longer complains, but the project won't run. How have I failed at coercing my gem jar into an actual compressed jar file?

The issue is that the -i flag on jruby is not creating a jar. That was a false assumption on my part. The correct method is this
java -jar jruby_complete.jar -S gem install -i gem_jar nokogiri erubis
jar cf gem_jar.jar -C gem_jar .

Related

Java - No Main Manifest Attribute, in App.jar error even though MANIFEST.MF file is in /META-INF

I have been trying to get this to work, but it won't. I went through the Archive Manager and found the MANIFEST.MF file in App.jar/META-INF/ so I don't know why it says there isn't one!
chmod-ing it with chmod +x ./App.jar and running ./App.jar returns
bash: ./App.jar: cannot execute binary file: Exec format error
Running Linux Mint 20 (Based on Ubuntu 20.04) With OpenJDK 11.
Use java -jar App.jar to run the executable JAR file.
There is a way to make JAR files directly executable using the binfmt_misc kernel feature but most users/linux distributions don't bother to set it up.

How to run `jdeprscan` on an EAR

The jdeprscan tool determines lists all deprecated and non-existing dependencies. It can run on classes, directories and on a JAR.
But how to run it on an EAR ?
Inspired by https://stackoverflow.com/a/57217414/698168, I explode the EAR into JARs using the following script (Windows) :
rem remove previous run
rd /s /q ear
rem extract the EAR
"C:\Program Files\7-Zip\7z" x -oear *.ear
rem extract the WAR
cd ear
"C:\Program Files\7-Zip\7z" x -owar *.war
rem unify JAR from EAR and WAR
copy war\WEB-INF\lib\*.jar lib
rem make JAR with the classes
cd war\WEB-INF\classes
rem "C:\Program Files\7-Zip\7z" a -r my-app.jar
"C:\Program Files\Java\jdk-11\bin\jar" cvf my-app.jar -C . .
rem Note: using 7zip to create the JAR may lead to errors when running jdeprscan, thus we are using the jar command
copy my-app.jar ..\..\..
rem return to origin
cd ..\..\..
rem unpack all libraries...
cd lib
"C:\Program Files\7-Zip\7z" x -aoa -oclasses *.jar
rem .. and repack them as a fat JAR
cd classes
rem "C:\Program Files\7-Zip\7z" a -r 00lib.jar
"C:\Program Files\Java\jdk-11\bin\jar" cvf 00lib.jar -C . .
rem duplicate the fat JAR and make some cleaning
copy 00lib.jar ..\00lib.jar
copy 00lib.jar ..\01lib.jar
cd ..
rd /s /q classes
rem return to origin
cd ..\..
Note that this script does not use the librairies from the JEE Server (i.e. all the Maven librairies with scope "provided" will be reported as error: cannot find class by jdeprscan).
Then I generate a jdeprscan report using the following command :
"C:\Program Files\Java\jdk-11\bin\jdeprscan" --for-removal --verbose --class-path ear\lib\*.jar ear\my-app.jar > deprscan.log 2>&1
You can then inspect the jdeprscan.log file. The classes that are not found may not exist in the newest Java version (such as 11) or may be present in the JEE modules. A missing class looks like the following (BASE64Encoder is not provided anymore by Java 11 but is used by ChecksumHelper):
Processing class oracle/spatial/security/ChecksumHelper...
error: cannot find class sun/misc/BASE64Encoder
In the best case, you can find the JAR name above in the log file (e.g. Jar file my-lib-2.3.4.jar), otherwise you will need to determine the library from the class name.
Note: all the above was designed with the idea to migrate Java 8 to Java 11.

How to create symlink in rpm to files provided by external rpms?

I'm trying to package two symlinks to corresponding jars provided by external dep1.rpm and dep2.rpm. These are declared as dependencies in my-app-extra.spec:
Requires: dep1
Requires: dep2
I have my %install symlinking their jars (%dep1jar, %dep2jar):
%install
%{__install} -d -m 0755 $RPM_BUILD_ROOT%{_javadir}
%{__install} -d -m 0755 $RPM_BUILD_ROOT%{_javadir}/%{my_app}
pushd $RPM_BUILD_ROOT%{_javadir}/%{my_app}
for jar in %dep1jar %dep2jar
do
ln -sf ../"${jar}" "${jar}"
done
popd
but the rpmbuild fails with:
ERROR: link target doesn't exist (neither in build root nor in installed system):
/usr/share/java/my-app/dep1.jar -> /usr/share/java/dep1.jar
Add the package providing the target to neededforbuild and Requires
// same error for dep2.jar
error: Bad exit status from /var/tmp/rpm-tmp.31253 (%install)
Is there any way to circumvent this rpmbuild check?
Background
I have a java application that (optionally) requires two external libraries to provide extended functionality. To install these libraries, I have created dep1.rpm and dep2.rpm that store the jars under %_datadir/java and an additional my-app-extra rpm that should symlink those under %_datadir/java/my-app (following some apparently established java-packaging conventions under linux).
But I'm failing to find how to force rpmbuild to build this rpm without declaring the external libraries as BuidRequires and having them installed first.
What I want
This is in summary what I'm after (simplified, as the complete solution involves versioning):
/usr/share/java/dep1.jar (from dep1.rpm)
/usr/share/java/dep2.jar (from dep2.rpm)
/usr/share/java/my-app/dep1.jar -> ../dep1.jar (from my-app-extended.rpm)
/usr/share/java/my-app/dep2.jar -> ../dep2.jar (from my-app-extended.rpm)
Note: I'm now exploring the possibilities of rpm subpackages, but I somehow expect this to be possible without subpackaging.
I could make it work 'touching' the targets and excluding them from the files. In my-app-extra.spec:
%install
%{__install} -d -m 0755 $RPM_BUILD_ROOT%{_javadir}
%{__install} -d -m 0755 $RPM_BUILD_ROOT%{_javadir}/%{my_app}
pushd $RPM_BUILD_ROOT%{_javadir}/%{my_app}
for jar in %{dep1jar} %{dep2jar}
do
# touch! rpmbuild aborts the build if symlink targets missing!
touch ../"${jar}"
ln -sf ../"${jar}" "${jar}"
done
popd
# more stuff ...
%files
# ...
# Exclude dummy (touched) files!
%exclude %{_javadir}/%{dep1jar}
%exclude %{_javadir}/%{dep2jar}
%{_javadir}/%{my_app}/%{dep1jar}
%{_javadir}/%{my_app}/%{dep2jar}

compile files from different directories with javac, referring a depending jar file?

I have the following set up:
I have 4 packages:
root/src/terminal - has some java files
root/src/mail - has some java files
root/src/data - has some java files
root/src/main - has a single java file, Main.java
I also have the following files
root/bin - a folder to store .class files
root/mail.jar - a jar file which has important classes used in my code
Within the root, I would like to enter a terminal command which compiles root/src/main/Main.java and puts the class files in the root/bin location.
Can someone show me the command to do this? I'm on a Mac (running Leopard).
Here's the one liner:
cd /xyz/root
rm -rf bin/*
javac -d bin -classpath mail.jar -sourcepath src main/Main.java
Alternatively, you could use absolute directory names:
rm -rf /xyz/root/bin/*
javac -d /xyz/root/bin -classpath /xyz/root/mail.jar \
-sourcepath /xyz/root/src /xyz/root/ main/Main.java
In reference to Ant you said "I would rather keep it simple.".
In fact in the long term it is simpler to create a simple Ant build.xml file. The alternative is a bunch of non-portable scripts or batch file ... or lots of typing.
To run the application, assuming that you are still in the /xyz/root directory:
java -classpath bin:mail.jar main.Main
Or on Windows:
java -classpath bin;mail.jar main.Main
Or modify the above to use absolute pathnames in the classpath argument; e.g.
java -classpath /xyz/root/bin:/xyz/root/mail.jar main.Main
Without knowing your operating system?
What you should look into is using Apache Ant. It is a build tool that once installed and configured can utilize a build.xml file in your root to compile class files to a folder as well as package a jar file.
http://ant.apache.org/
try this:
javac -cp "/root/mail.jar;/root/src;" -d "/root/bin" Main.java
This is written hoping that you have package declarations in your classes from src folder like package terminal; and package main;.
See this: Options in javac command
Or use Apache Ant as suggested by maple_shaft.
From comment give by #maple_shaft:
In Unix, Linux operating systems the classpath separator is a colon instead of a semicolon.

Comparing two .jar files

How do I compare two .jar files?
Both of them have compiled .class files.
I want the difference in terms of method changes, etc.
JAPICC, sample usage:
japi-compliance-checker OLD.jar NEW.jar
Sample reports for log4j: http://abi-laboratory.pro/java/tracker/timeline/log4j/
PkgDiff, sample usage:
pkgdiff OLD.jar NEW.jar
See sample report for args4j.
Clirr, sample usage:
java -jar clirr-core-0.6-uber.jar -o OLD.jar -n NEW.jar
If you select two files in IntellijIdea and press Ctrl + Dthen it will show you the diff. I use Ultimate and don't know if it will work with Community edition.
Rename .jar to .zip
Extract
Decompile class files with jad
Recursive diff
Extract each jar to it's own directory using the jar command with parameters xvf. i.e. jar xvf myjar.jar for each jar.
Then, use the UNIX command diff to compare the two directories. This will show the differences in the directories. You can use diff -r dir1 dir2 two recurse and show the differences in text files in each directory(.xml, .properties, etc).
This will also show if binary class files differ. To actually compare the class files you will have to decompile them as noted by others.
Create a folder and create another 2 folders inside it like old and new. add relevant jar files to the folders. then open the first folder using IntelliJ. after that click whatever 2 files do you want to compare and right-click and click compare archives.
I use to ZipDiff lib (have both Java and ant API).
Here is my script to do the process described by sje397:
#!/bin/sh
# Needed if running on Windows
FIND="/usr/bin/find"
DIFF="diff -r"
# Extract the jar (war or ear)
JAR_FILE1=$1
JAR_FILE2=$2
JAR_DIR=${PWD} # to assign to a variable
TEMP_DIR=$(mktemp -d)
echo "Extracting jars in $TEMP_DIR"
EXT_DIR1="${TEMP_DIR}/${JAR_FILE1%.*}"
EXT_DIR2="${TEMP_DIR}/${JAR_FILE2%.*}"
mkdir ${EXT_DIR1}
cd ${EXT_DIR1}
jar xf ${JAR_DIR}/${JAR_FILE1}
jad -d . -o -t2 -safe -space -b -ff -s java -r **/*.class
cd ..
mkdir ${EXT_DIR2}
cd ${EXT_DIR2}
jar xf ${JAR_DIR}/${JAR_FILE2}
jad -d . -o -t2 -safe -space -b -ff -s java -r **/*.class
cd ..
# remove class files so the diff is clean
${FIND} ${TEMP_DIR} -name '*.class' | xargs rm
# diff recursively
${DIFF} ${EXT_DIR1} ${EXT_DIR2}
I can run it on Windows using GIT for Windows. Just open a command prompt. Run bash and then execute the script from there.
Use Java Decompiler to turn the jar file into source code file, and then use WinMerge to perform comparison.
You should consult the copyright holder of the source code, to see whether it is OK to do so.
In Linux/CygWin a handy script I use at times is:
#Extract the jar (war or ear)
cd dir1
jar xvf jar-file1
for i in `ls *.class`
do
javap $i > ${i}.txt #list the functions/variables etc
done
cd dir2
jar xvf jar-file2
for i in `ls *.class`
do
javap $i > ${i}.txt #list the functions/variables etc
done
diff -r dir1 dir2 #diff recursively
If you are using IntelliJ IDEA or Android Studio, add your jar files to a project under the libs folder.
Then select the both jar files, right click then select "Compare Archives"
use java decompiler and decompile all the .class files and save all files as project structure .
then use meld diff viewer and compare as folders ..
Here's an aparently free tool http://www.extradata.com/products/jarc/
Please try http://www.osjava.org/jardiff/ - tool is old and the dependency list is large. From the docs, it looks like worth trying.
This application may be what you need, works great and display a simple GUI showing differences. Try Jarcomp

Categories