IKVM.NET & Excel VBA - java

I'm using IKVM.NET in order to convert a Java library (signally, Strata) into a .NET library. Here are the steps I perform:
I download the latest Strata release.
I unzip all the JAR files contained into the lib folder of the archive to C:\Strata\, including the following auxiliary libraries:
commons-math3-3.6.1.jar
guava-26.0-jre.jar
joda-beans-2.4.0.jar
joda-convert-2.2.0.jar
slf4j-api-1.7.25.jar
I generate a keyfile for the library I want to create using the command sn -k "C:\Strata\Strata.snk".
Using the binary bytecode compiler of IKVM.NET, I convert the JAR files of Strata into a .NET library with the following command: ikvmc -out:"C:\Strata\Strata.dll" -keyfile:"C:\Strata\Strata.snk" -assembly:Strata -version:2.2.0 -fileversion:2.2.0 -target:library -platform:anycpu -recurse:"C:\Strata\*.jar".
Once the process described above is done, even if a few warnings concerning missing classes are shown, I obtain a working .NET wrapper of Strata. If I create a new Visual Studio project referencing the Strata.dll assembly, I'm capable of using its classes and methods without problems.
What I would really love to achieve is to make the wrapper work in Excel VBA macros, so that I can instantiate and use Strata classes in the subs, according to the contents of the sheets.
This is what I tried so far, but to no avail:
I register all the IKVM.NET libraries and the Strata wrapper into the GAC as follows: gacutil /i "C:\IKVM\IKVM.*.dll", gacutil /i "C:\Strata\Strata.dll".
I register the Strata wrapper as COM component and I create its types library as follows: regasm /codebase /tlb "C:\Strata\Strata.dll".
Now, when I open Excel and I go under Development > Visual Basic > Tools > References... I can see the TLB file of Strata (located at "C:\Strata\Strata.tlb") and I can add it to the current project. But as soon as I type something the window or I open the Objects Browser, Excel crashes without providing any meaningful information about what's going on.
I'm totally clueless about this issue.
Is my registration process correct? Do I have to register the IKVM.NET libraries too and create their type libraries? Should I include them into the Excel VBA project together with the Strata wrapper type libraries? Could the problem be caused by the fact that I'm using a x64 version of Excel and the wrapper has been compiled under AnyCPU? Do I need to edit the wrapper by adding a ComVisible attribute on every public class? May this problem be due to the fact that the wrapper contains weird method names like ā€œ\_Build01_\pā€?

Rather than use IKVM.NET you can use Java directly to build an Excel add-in.
To build an Excel add-in in Java instead of having to use .NET, see https://exceljava.com.
There is even a Strata-Excel project that you might be interested in that wraps the Strata library in an Excel add-in: https://github.com/exceljava/strata-excel
I think you should find this much more convenient than converting the Java libraries to .NET.

Related

Java code reuse with pre compiled class including Main

I downloaded all the Apache POI downloadables recently, specifically poi-examples-3.11-20141221.jar wherein it includes pre-compiled examples like "How to Use".
The problem is I can't run the pre-compiled classes without Eclipse.
Specifics: poi-examples-3.11-20141221.jar
-> org.apache.poi.xssf.eventusermodel
-> XLSX2CSV.class
XLSX2CSV is already compiled with main() and I just want to simply run it without eclipse.
Links through other tutorials about JAVA Reference Class and Jar in Java will also be helpful.
Im new here so please be gentle.
Run it with the jar on the classpath, but not with the -jar option
For example, for the .xls to csv converter example XLS2CSVmra you'd do something like:
java -classpath poi-3.12-beta1.jar:poi-examples-3.12-beta1.jar org.apache.poi.hssf.eventusermodel.examples.XLS2CSVmra
Make sure that all the POI jars you need are on the classpath (.xlsx / XSSF needs more), along with any of their dependencies from the lib directory. See the POI Components Page for details of what jars you need for what

Converting dll to jar

I'm trying to find a way to convert a dll to a jar file. I have a .net application that communicates with a java application. The core entities are .net objects which I have to duplicate manually in java.
I've read about IKVM but it seems that it converts only jars to dlls and not the other way around.
Edit: If there is a tool that creates java classes from a dll it is also fine.
Thanks in advance
There isn't such a tool.
A dll is a natively compiled library. That means its been compiled down to machine code. Probably compiled by a C/C++/C# compiler.
A jar file is a zip file that contains '.class' files, which are files compiled down to 'java virtual machine code'. Probably compiled by a java/clojure/scala compiler.
These are two very different incompatible things.
It's not impossible to create such a tool that would do this translation, but it would definitely be an extremely difficult task, as it would entail translating from one machine code, to another, and would need to manage multiple issues like dependency solving, different type structure etc.
HOWEVER, I'm imagining that you want to do this because you want to use a DLL within some java code. That is somewhat possible, but is actually quite complicated. You'll need to use the JNI.
Take a look at this question as it might help you achieve what you want to do:
Calling C++ dll from Java
This is actually an easy task to perform. Converting .dll to .jar is as simple as using com4j and a couple of commands on the command line.
Download com4j.
Open command line and navigate to com4j directory in above step.
Execute below command.
java -jar tlbimp.jar -o outputFolder -p nameOfPackage "pathToFile"
Then jar the results with the following:
jar cf desiredJarName.jar folderYouWantJard

Do Java programs natively support the concept of version?

I was wondering if there is any standard procedure to set a Java project's version (a bit like a Windows executable file version).
I'd like to have my application's code able to have introspection capabilities so it can know its own version.
Is it possible, at all, to do this with Java without resorting to have a .txt file in the directory with the version as contents?
If yes, the ideal would be to have Eclipse update the version each time it is compiled. Is this possible?
The Java native way to store version information is in the manifest files (this part of the tutorial is about version information).
You can use the Package class and its methods such as getImplementationVersion to get at those details at runtime.
I'm not sure how to tell Eclipse that I want it to create a .txt file in a given place each time it automatically builds a .java file, though..
Can I suggest that you take a look at Maven, and the release process that it supports.

Converting a Scala library to a DLL (.NET)

I'm trying to create a Dll out of a scala-class. I'm using IntelliJ together with SBT. I've already found a way to convert .jar files into a Dll, using the ikvm-converter. Now the problem: When I use "package" under SBT to create a .jar file out of my .scala file and try to convert it afterwards with ikvmc into a Dll the resulting library is empty when integrated in C#...
For example converting the Jama-Library (which is written in Java) works fine, where converting Scama (written in Scala) does not work.
Is there a way to do this conversion of scala code into a dll? Is there a "Scala to Java"-conversion tool?
Best Regards,
Christoph
I have no knowledge of .NET, but judging from SK-logic's and your comments to the questions: sbt package does not include the Scala runtime library, because it assumes you are going to export your project as a library to be used within other Scala projects.
Therefore, you will need to create a "fat" jar that contains the runtime. For example, in this blog you can see how the author creates a fully self-contained executable, by converting both the project jar and the runtime jar.
There are different tools to do that with sbt. The easiest would be sbt-assembly, but you will end up with a very large file, because it just adds the whole runtime. If that is a problem, you may want to filter the runtime instead, using the proguard plugin. More on this topic in another StackOverflow entry.

Managing a ZIP-based compression file in Java

I know that Java natively supports the ability to navigate through and ope files compressed within a ZIP file, as that is what a JAR file is. How can I utilize this to make and manage a ZIP-based file (for saving a program's state)?
Please note that the project I am working on aims to complete its goal WITHOUT ANY EXTERNAL LIBRARIES. All libraries must be already included in Java 1.6 or 1.7.
You can use the java.util.zip package, part of the Java standard library since JDK 1.1.
See java.util.zip.

Categories

Resources