How to import classes with Ruby Java Bridge - ruby

I have a jar file that has a class that I would like to use from my Rails project. I have tried to import the class with Rjb with these commands. The jar file is in the bin directory.
Rjb.load("#{Rails.root}/bin")
=> nil
Rjb::add_jar("excel_tools.jar")
=> true
Rjb::import("tools.CellEditor")
ClassNotFoundException: tools.CellEditor
Rjb::import("tools/CellEditor")
ClassNotFoundException: tools.CellEditor
The class name "tools.CellEditor" should be correct. At least when I list the classes in the jar in terminal I get this and many more classes from apache poi and log4j.
$ jar tvf bin/excel_tools.jar
6926 Mon Aug 25 13:24:00 EEST 2014 tools/CellEditor.class
Any idea where the jar or class loading goes wrong?

I got this working by importing all the jar files my java class depends on. It is not enough that the dependence jars are bundled into another jar file with my class, they need to be loaded separately before importing the class it self.
I copied all the jars into a java_libs directory and wrote an initializer like this
require 'rjb'
JARS = Dir.glob("#{Rails.root}/lib/java_libs/*.jar").join(':')
Rjb::load(JARS)
CELL_EDITOR = Rjb::import('tools.CellEditor')
Now I can use the CELL_EDITOR methods anywhere in my code.

Related

Generate and run Maven Kotlin jar

I'm trying to generate a jar file of my kotlin project.
I read Maven Kotlin and copied the code into my pom.xml but I don't understand what I'm supposed to insert at ${main.class}.
Here is my code architecture with MorisKt.java as my main class.
I tried MorisKt.class, /MorisKt.class, target/Moris.class(container folder of my compiled code)
And nothing worked. The jar is generated but I have Could not find or load main class when I try to run it.
If you can explain me where I'm doing it wrong
You need to put your main class file in a package and then reference it with the fully qualified name.
For instance if you put your Moris.kt in a package named app it would be :
<mainClass>app.MorisKt</mainClass>

How can I submit an Apache Storm topology to a Storm cluster?

I'm following this tutorial: https://learn.microsoft.com/en-us/azure/hdinsight/storm/apache-storm-develop-java-topology
What I've done so far is
maven setting
vi *.java files (in src/main/java/com/microsoft/example directory)
RandomSentenceSpout.java
SplitSentence.java
WordCount.java
WordCountTopology.java
mvn compile
jar cf storm.jar *.class (in target/classes/com/microsoft/example directory)
RandomSentenceSpout.class SplitSentence.class WordCount.class WordCountTopology.class
The above 4 files were used to make storm.jar file
Then, I tried
storm jar ./storm.jar com.microsoft.example.WordCountTopology WordCountTopology
and
storm jar ./storm.jar WordCountTopology
, but both of these failed, saying:
Error: Could not find or load main class com.microsoft.example.WordCountTopology
or
Error: Could not find or load main class WordCountTopology
According to a document, it says
Syntax: storm jar topology-jar-path class ...
Runs the main method of class with the specified arguments. The storm
jars and configs in ~/.storm are put on the classpath. The process is
configured so that StormSubmitter will upload the jar at
topology-jar-path when the topology is submitted.
I cannot find where to fix.
How can I resolve this?
I think your jar file does not contain class WordCountTopology. You can check it with jar tf storm.jar | grep WordCountTopology.
Looks like your jar does not contain a Manifest file which keeps information about the main class.
Try including the Manifest file or you can run the below java command to include the Manifest file
Hope this works!
jar cvfe storm.jar mainClassNameWithoutDotClassExtn *.class

Unable to change Main-Class property within Manifest.mf

I am upgrading a springboot app from 1.X to 2.1.3.RELEASE. My Springboot is a multi module project setup which is triggered using a java command something similar to this.
java -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5088 -Drun.jvmArguments="-Xdebug" -Dloader.path=C:\code\build\libs\dir\ -jar C:\code\build/libs/core.jar --spring.config.location=C:\config\application.properties
This is how the project structure looks like
App
Common
Core (has the main class)
Sub
Before the upgrade : Manifest.mf file of core.jar looks like this
Manifest-Version: 3.2.15.signature.LOCALDEV
Project-Name: core
Built-By: dkumar
Built-Date: 2020-11-10 16:09
Project-Version: 3.2.15.signature.LOCALDEV
Spring-Boot-Version: 1.5.1.RELEASE
Main-Class: org.springframework.boot.loader.PropertiesLauncher
Git-Branch: signature
Start-Class: com.demo.Main
Spring-Boot-Classes: BOOT-INF/classes/
Git-Commit-Hash: signature
Project-Group: com.demo.print
Spring-Boot-Lib: BOOT-INF/lib/
After the upgrade. This what the manifest.mf file looks like
Manifest-Version: 3.1.87.upgradespring.LOCALDEV
Git-Branch: upgradespring
Project-Name: core
Built-By: dkumar
Built-Date: 2020-11-18 18:15
Start-Class: com.demo.Main
Git-Commit-Hash: upgradespring
Project-Group: com.demo.print
Project-Version: 3.1.87.upgradespring.LOCALDEV
Main-Class: org.springframework.boot.loader.JarLauncher
Before the upgrade the following property in my Core build.gradle took care of using org.springframework.boot.loader.PropertiesLauncher which inturn used -Dloader.path to load all the Sub (subprojects) jars on to the classpath
springBoot{
mainClass = "com.demo.Main"
layout = "ZIP"
}
After the upgrade, the layout property is now deprecated in 2.1.3.RELEASE and the Main-Class is now changed to Main-Class: org.springframework.boot.loader.JarLauncherand this doesn't support the usage of -Dloader.path (I think). Because of this I always get No bean found error when i try to invoke a class of Sub (subproject). This happens because -Dloader.path fails to include the JARS from subproject onto the classpath. Before the upgrade I could see the relevant jars on the class path.
I tried several ways to change the Main-Class attribute within the manifest to use PropertiesLaucher but none seem to have worked. I have also looked at the plugin documentation and tried something similar but that to didn't work
https://docs.spring.io/spring-boot/docs/2.1.3.RELEASE/gradle-plugin/reference/html/#packaging-executable-configuring-properties-launcher
I think by changing the Main-class I can solve this problem but I am not sure how to do that.
Any suggestions or idea would be helpful. All comments are welcome. I will update the question if the need arises.
I apologize in advance as i am unable to place a larger code snippet due corporate policies.
I was able to resolve this by adding the following properties
bootJar {
manifest {
attributes(
'Main-Class': 'org.springframework.boot.loader.PropertiesLauncher',
'Spring-Boot-Classes': 'BOOT-INF/classes/',
'Spring-Boot-Lib': 'BOOT-INF/lib/'
)
}
}
Only this should work as well within the manifest:
'Main-Class': 'org.springframework.boot.loader.PropertiesLauncher'

GMavenPlus: Groovy to JAR - Failed to find or load main class. Main class is present, manifest file appears valid

I've done a successfull mvn clean install on the project I built and the structure appears correct, all classes included and manifest is under META-INF including class-paths and main-class. Not sure what's not matching up here, but the class contents are valid when I checked contents using javap. Main method is present in the redacted_automatedSupport class and is also public.
Error:
host MINGW64 ~/Desktop/Projects/redacted_Automated_Support/target (master)
$ java -jar redacted_automatedSupport-1.0.jar
Error: Could not find or load main class support.redacted_automatedSupport
host MINGW64 ~/Desktop/Projects/redacted_Automated_Support/target (master)
$ java -cp redacted_automatedSupport-1.0.jar support.redacted_automatedSupport
Error: Could not find or load main class support.redacted_automatedSupport
host MINGW64 ~/Desktop/Projects/redacted_Automated_Support/target (master)
$
Manifest contents:
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: bennet.vella
Class-Path: aws-java-sdk-s3-1.11.696.jar aws-java-sdk-kms-1.11.696.jar
aws-java-sdk-core-1.11.696.jar commons-logging-1.1.3.jar httpclient-
4.5.9.jar httpcore-4.4.11.jar commons-codec-1.11.jar ion-java-1.0.2.j
ar jackson-databind-2.6.7.3.jar jackson-annotations-2.6.0.jar jackson
-core-2.6.7.jar jackson-dataformat-cbor-2.6.7.jar joda-time-2.8.1.jar
jmespath-java-1.11.696.jar groovy-3.0.1.jar groovy-json-3.0.1.jar gr
oovy-dateutil-3.0.1.jar
Created-By: Apache Maven 3.6.3
Build-Jdk: 1.8.0_231
Main-Class: support.redacted_automatedSupport
Jar Archive Contents (renamed to zip to browse):
Support Jar Contents (removed some unecessary data, all names match):
It is not a complete answer, since it doesn't target the Maven issue I was having, but I did solve the IntelliJ problem I had with the wrong manifest file - and that's because I was creating the manifest in src/main/java when it should have been src/main/resources. This should hopefully alleviate some users' problems.
I have not however resolved how to properly build and include all relevant dependent jars using Maven - intelliJ does this successfully.

Building Java Project with Hadoop-LZO but cannot find class

I'm trying to build a simple WordCount jar project which utilizes Hadoop-lzo library but cannot seem to get the following command to work, even though the class I'm referencing is within hadoop classpath:
$ javac -cp `hadoop classpath` *.java
LzoWordCount.java:76: cannot find symbol
symbol : class LzoTextInputFormat
location: class LzoWordCount
job.setInputFormatClass(LzoTextInputFormat.class);
^
1 error
Any ideas?
I assume you have correctly installed your LZO libraries (you should have libgplcompression.so in your lib/natives/Linux**-**/ and the jar file in your lib/ folder)
Since you have them the correct class should be LzoDeprecatedTextInputFormat.class or LzoTextInputFormat.class depending on wich API you use (According to your post you are using it right Job with LzoTextInputFormat).
So your problem could be in your java.library.path, wich should include path to your jar file. You can set it up in your .bash_profile or in you bin/hadoop file.
hope that helps.

Resources