Java code reuse with pre compiled class including Main - java

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

Related

How to change compile only one class of a java source code

I am using Apache POI library on Java. I want to add a method to XWPFSDTContent class. I have the source code but when I want to compiling after adding method I have to use "ant jar" command but it takes so much time and makes it harder to debug because it compiles the whole library. Is there a way to compile only one source file? I can't use javac command because of its dependency on the library.
Assuming you have the source code of Apache POI imported into a java project in some kind of IDE. Then you basically only need to edit your one class, afterwards the IDE should have generated the new .class file.
Take this class file (and any possibly existing anonymous inner class files) and copy it into your jar (replacing the old .class files). That should work assuming that you compiled the class file with the same java version as the rest of the classes inside the jar and your jar is not signed.

IKVM.NET & Excel VBA

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.

How to use a Tika custom parser in a jar file?

I'm trying to write a custom Apache Tika parser (for DICOM medical images), and package it as a plugin in a jar file.
I'm following the instructions from http://tika.apache.org/1.18/parser_guide.html, and took these projects as models:
https://github.com/Gagravarr/VorbisJava (the tika part),
https://github.com/Gagravarr/MPXJ-Tika.
So, I created a Maven project, wrote a parser class and a org.apache.tika.parser.Parser file in the resources folder, built the project with mvn install, and I now have a jar file.
My question is, how do I make Tika use this new parser? The instructions on the Tika wiki say:
To install a plugin, download it according to instructions below and
drop the jar(s) on your classpath. Tika will auto detect the plugin.
I tried to do this with java -classpath /path/to/my-parser.jar ... but it doesn't seem to work:
java -classpath /path/to/my-parser.jar -jar tika-app-1.18.jar --list-parsers
doesn't list the new parser, for instance.
I'm not a java person, and I'm really not sure about what "drop the jar on your classpath" means. I would really appreciate if someone could point me to the right direction! Thanks.
You've sadly made a common Java newbie mistake - for various historic reasons the java program won't accept both -jar and -classpath options, and will ignore the -classpath parts you've given.
If you want to run the Apache Tika App on the command line, with an extra parser jar or two added, what you need to do is something like:
java -classpath tika-app.jar:my-extra-parser.jar org.apache.tika.cli.TikaCLI --list-parsers
That calls the main Tika App entry point (the default with -jar) when running with both the Tika App jar and your custom extra jar on the classpath.
You may also find the Troubleshooting Apache Tika guide from the Tika wiki useful when developing custom plugins like this!

Can't open a .jar file on my mac

I'm trying to download jsoup on my mac (Mountain Lion). I've downloaded the jsoup.jar file and installed the last java 7 from the site. But here is the problem, when I double click the .jar file it tells to me:
The Java JAR file “jsoup-1.7.2.jar” could not be launched. Check the
console.
I can't find even the console! Someone can help me? I read a lot of answers about this topic, but they all talk about Java 6 and it has different settings that can't find.
EDIT
i also tried from the terminal with this command:
java -jar /Users/Ben/Downloads/jsoup-1.7.2.jar
but it tells me:
Failed to load Main-Class manifest attribute from
/Users/Ben/Downloads/jsoup-1.7.2.jar
The JSoup JAR is not executable, so you are not going to be able to 'run' it in any of the ways you described. You are supposed to include it in your project classpath and use classes from it to do your parsing (after importing them of course).
You might want to refer to the JSoup Guide for examples on using the library.
I guess you are trying to run the jsoup library as a standalone application assuming it to be an executable jar. All indicates that the jar file you are using is NOT an executable jar hence it wont work.
jsoup.jar is supposed to be used as a java library and you will need to write java code to be able to use the HTML parsing capabilities.
If you are using an IDE like IntelliJ, you can open the module settings for a particular project and select Libraries. There'll be an option to add a particular external library from the Maven repository after which you can download the JAR and include it in your project's dependencies.

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.

Categories

Resources