I'm an entry level programmer so please be descriptive in your responses.
I am trying to use a Java API given as a .jar file in my C# .net application. I don't know much Java, but this .jar file says "no main-class manifest attribute" when I try to run it so this means its a library? This Java API also comes with a .java file which shows how to use the library but I cannot see the code inside the .jar.
I found this question on this site, and one of the answers reads, "In simple way you can pack your java classes to jar file then In C# use Process class for execute and map IO stream." I am semi-familiar with the Process class in C# but I don't understand how I could use it to use a Java library in my C# .net project.
Is this possible? or was that answer incorrect?
If so, could you explain how I can use the .jar library in my C# app.
You can do it using IVKM.Net. IVKM.NET includes an application called ikvmc. Here’s the documentation for this tool:
http://www.ikvm.net/userguide/ikvmc.html
To use it compile your java code into a Jar.
Then run the ikvmc program:
ikvmc myCode.jar
If your jar contains a main() function, it will be converted into an exe that can be run on the CLR. Otherwise it will be converted into dll’s. To use the dll’s in your project just add them as references in Visual Studio and the same API that was available to you in Java will be accessible in your .Net application.
You can also include the IKVM.GNU.Classpath.dll dll from ikvmc and use the standard java class libraries in your application.
Have a look at IKVM ... it has tools to give you some level of interop. When you say Java API I assume you want to call some functionality from the jar rather than just execute it
You could use IKVM.NET - http://www.ikvm.net/userguide/ikvmc.html
On the official website in download - you can get
ikvmbin-7.2.4630.5 (Works up to Java 7)
However, on the owner's blog, you can download a newer version.
http://weblog.ikvm.net/default.aspx - You can get
ikvmbin-8.1.5717.0 (Works up to Java 8)
To create dll/exe use:
ikvmc hello.jar
On the other hand, if you can edit .jar lib (you created it) you
could use http://jni4net.com/ project.
Related
I have a compiled DLL (C/C++) and I have to use it in Java. So I want to use SWIG to create a wrapper . I followed all the tutorials like:
How to create a DLL with SWIG from Visual Studio 2010
http://www.swig.org/Doc3.0/Java.html
SWIG JNI Interface with DLL and declarations only
But in all the examples no precompiled DLL and/or Java and/or Visual Studio (2015) were used. When using an own cpp file for the implementation (and don't use an external DLL, implementing the header) everything works fine (compile to a single DLL and calling from Java).
So what do I want: Having a precompiled DLL (example.dll), the header for that (example.h) and the SWIG header file (example.i). Compile everything to a "wrapper DLL", load this one in Java and accessing the precompiled DLL (example.dll).
My steps in detail:
New VS class library project
Add the h and i file
Add the custom build step for the i file (like in the example)
Java files and the example_wrap.cxx file is generated
Add the example_wrap.cxx, add Java includes and don't use precompiled headers
Compile example_wrap.cxx to example.lib (Is that right?)
Setting project settings like in the tutorials (no clr, add java-includes, no precompiled headers)
Now creating the whole project fails with linking error LNK2019 in example_wrap.obj.
Can someone help me?
What I also don't get: The result will be "example.dll". I have to use this DLL in Java (with System.load/loadLibrary). How can this SWIG DLL access the "original" example.dll with all the logic inside?
Using Python instead of Java results in the same failure (LNK2019 - unresolved external symbol).
Basically you need to "forward" the calls to the external library. I.e. import symbols from there into the wrapper library and link with the external DLL lib file.
If you do not have such a lib file for the DLL, you need to either create it (e.g. using some tool the create lib from the DLL), or load the external library/import symbols at runtime (LoadLibrary/GetProcAddress).
The answer from axalis was absolutly right. Here some details:
Add example.cpp
Load the precompiled DLL (example.dll) with LoadLibrary and save a reference
Load methods from there with GetProcAddress and save a reference for each method
Write an "implemantation" for each method where you just call the method loaded before
Compile and link everything to exampleWrap.dll and load this dll in the Java project
i started to learn android program ,and i curious to see the code of the classes/
for example the class of import android.app.Activity.
how can i see the source code of the packages in eclipse?
You can attach source code to libraries. yourProject / Properties / Java Build Path / Libraries / yourLibrary / Source attachment, and then enter the folder or zip file containing the source code (which you need to download separately).
If you only have the object code (.class files), then you need to learn to read ByteCode (which Eclipse shows pretty nicely) or use a Java ByteCode disassembler.
As in android sdk what we get is a compiled jar file which contains all the classes.
So in eclipse you can't read the source, but you may use, The official online version to read the source code of classes:
http://developer.android.com/reference/packages.html
[In eclipse although by CTRL+Click on import may show you a bit about the class, although that won't be easy to understand :)]
I am using mediainfo (http://mediainfo.sourceforge.net/en) to extract information from audio and video files using Java code.
My java project runs over all platforms (osx, win & linux). So far I have only tested mediainfo over osx where the procedure is simple: just put libmediainfo.dylib in the target folder and access it using a native library and you're good to go. And the solution works flawlessly...
I am now looking to expand this functionality to the other OSs, starting with Linux. However, it is proving to be more of a challenge than I thought.
At first I kept getting this:
"java.lang.UnsatisfiedLinkError: Unable to load library 'mediainfo': libmediainfo.so: cannot open shared object file: No such file or directory"
and this was expected as it was looking in /usr/lib
but this was solved by installing the suitable libmediainfo0 & libzen0 ".deb from http://mediainfo.sourceforge.net/en/Download/Ubuntu
Still, my solution needs to be portable, meaning, I want all necessary resources to be included with the java project package without requiring any further installations.
I also need to know if it's possible to change mediainfo to look for the resources in my java package instead of a system location.
For your reference, I am using Java Native Access (jna) to interact with the library. Also using the MediaInfo.java & MediaInfoLibrary.java classes that the website suggests.
Let me know if you need other details.
any wisdom is highly appreciated
thanks!!
The latest release of JNA (3.5.2) will automatically unpack native libraries bundled as resources (whether file- or jar-based).
If you include you shared library for linux/amd64 as /linux-x86-64/libmylibrary.so in one of your jar files, JNA will extract it and load it automatically when you call Native.loadLibrary("my library"). Older versions of JNA require that you make the library available on LD_LIBRARY_PATH (envariable) or jna.library.path (system property).
I need to create runnable .jar file programmatically from a string. My decision is to create a .class file from string and add it to the .jar using JarOutputStream.
What API must I use to create the .class file?
Are there any other solutions to create a .jar from the source code?
In order to do that, you can use the Java Compiler API.
There is this excellent tutorial that can walk you through.
To compile code, you need a compiler. You can either use the SunOracle compiler or the Eclipse compiler. Calling the compiler API (both have documented APIs) will produce a .class file in a temporary location. You can then make a jar.
For an example of this sort of thing, start with, for example, the Maven Compiler Plugin, which is a Java module which uses the compiler API. You'll have to find your way into the Plexus compiler module.
We have some proprietary libraries we need to interface with. These libraries are Windows DLLs, or Linux .so files. We got the headers to define the interfaces. Since I have never done anything with native libs, I looked at JNAerator (http://code.google.com/p/jnaerator/) and the BridJ and JNA stuff.
What's a simple way to use a C++ header file and the compiled lib to generate an interface? For example, by adopting JNA in general with something like:
SomeDLL lib = (SomeDLL) Native.loadLibrary("some_dll", SomeDLL.class);
I have to keep the DLL somewhere: how do I bundle the DLL with the Jar? I use Maven to build the Jar file... but the Native.loadLibrary interface doesn't allow to directly specify a path.
JNI coding is usually a manual process of writing C++ code to create the native glue methods. There's an entire book that explains it.
In some cases, http://jna.java.net/ can automate or accelerate this process, but don't count on it.
You can't 'bundle the native libraries' unless you go down the path of using OSGi or something like the Tanukisoft packaging tool, there's no built-in feature for that purpose in Java.
You connect the dots by using -Djava.library.path to tell java where to find native libraries, or using the lower-level APIs to System.loadLibrary that allow you to specify a full path.
Watch out for interactions with PATH and LD_LIBRARY_PATH if your native libraries have dependencies in turn.
With BridJ, you can bundle the DLL/.so/.dylib just fine with the JAR, but you have to put it (or them) in a specific platform-dependent path within the JAR, which starts by "org/bridj/lib/" and ends with a platform+architecture identifier.
Here's BridJ's own source tree, which exhibits this native bundling scheme :
org/bridj/lib resource directory
If you stick to this convention, you won't have do deal with PATH, LD_LIBRARY_PATH or file extraction : BridJ.register() (called on a #Library-annotated class with native methods) will do it all for you !