I have created a NuGet package (.Net standard 2.0 and .Net Framework 4.8 - compatibility) that I would like to know if it is possible to access from a Java application.
Nuget is a package format that is used in .NET. Not in Java. Java has Maven for that.
So there is no simple way (or any way I could recommend) to use the code easily in Java.
But if you really need to use the methods you have in the package, you can try to do the following:
Compile the code that the package contains, so that you have .dll file
Try to make use of the compiled dll in Java. I don't know much Java but it seems this is already answered in Stack Overflow: How to call a method in DLL in a Java program
Basically you should be able to call the methods defined in your DLL. Similar thing you can do in .NET. It is possible to take a dll, and call it's methods from .NET knowing the method signature. It is called Interoperability
Also please note that the nuget package is a zip repository in fact. You can open it using any zip tool, you can extract it and compile the code with dotnet command.
Related
I am writing the python module where I need to interact with java module for some work.
I have already jar for java layer.
I can I invoke the jar files and call the class/Methods which are there in Jar files.
I don't want to use the Jython since major of my code is pure python
I tried subprocess.call()
but it's not serving my purpose
subprocess.call(['java','-jar', 'my.jar'])
EDIT:
I need to call the java layer because I need some input to my python module from there.
I tried py4j but no successes
JPype is an alternative to Jython that I made some good experience with. If it is not enough to call the java program and work with the output (it's hard to tell from your question), then JPype can be used to (more or less) transparently work with Java object in Python code.
I works by starting a JVM and handing requests to said JVM.
I am not a Java developer, but I need to solve a problem: I need to include linux SO library to an existing Java project. The library is developed using CGO and works fine with C++, Python ctypes, Ruby FFI and so on. But I can not include it to Java project without of errors (java.lang.UnsatisfiedLinkError). I have read some articles like this, and the described method is that I need use javah to create a C header first, and then write a C program, and so on. But what to do, if I have already compiled *.SO file?
Is there a way, how to simply load an existing SO file (written on C) and call it`s functions?
Java doesn't have builtin FFI functionality.
One option for using native libraries (.dll/.so) with Java is to write a JNI wrapper library that has special functions that can be bound to Java native methods. That's the option where you need to use javah to create a header file for the wrapper library. See the "Java Native Interface" documentation on Oracle's site for how to do that.
The other approach is to use a "glue" library like JNA. You don't need to build another library this way but you need to include JNA in your project and do the necessary Java declarations for it. You can find the documentation for JNA in the Github repository together with the code. That approach is similar to what Python, Ruby, etc. are doing.
I recommend reading up on both to see what will better suit your needs.
I'm looking to write a Java program which will download a Java source file in text format off the web, compile it, load it, and use it as part of the running program. I've heard this is possible, but don't know how to write the code to make it happen. A fully functioning example or tutorial would be great, if you could point me in the direction of documentation such as this.
Once I learn how it's done, I plan to use this knowledge to build an Android Application which can customize itself with code from the web.
A desktop program could use a shell script to download, compile and run a Java program. Android does not have a compiler, and adding one is non-trivial. The easiest way would be to also make a server program. The Android program would then tell the server program to download and compile the Java source code, and then send the result to the Android program, which would then load it using its ClassLoader.
One caveat is that the JDK compiler produces bytecode for the standard Java Virtual Machine, whereas Android's JVM is uses the Dalvik VM, so when you compile the Java class, you can't just use the JDK; you have to use the Android SDK to produce compatible bytecode that the Android ClassLoader can use.
Yes, it's possible in the general sense to do what you want. In the specific case of Android, however, it is likely that the sandbox imposes restrictions that will make what you're trying to do difficult or impossible. To do this in the general case you can use an approach like:
Use a web library of your choosing (for example, HttpClient, or HtmlUnit, or for simple tasks, Java's built-in URLConnection class is entirely acceptable as well) to download the Java file locally.
Use System.exec() to fork a javac process to compile the downloaded Java file for you (or use a JavaCompiler implementation to do the same). Note that this might be a bit tricky if the downloaded Java file uses external JAR's/libraries that aren't on your system.
Use the ClassLoader to load your compiled class. Note that you'll only be able to use it if your runtime classpath also includes any external JAR's/libraries that the code you're loading in relies upon.
However, step #2 will almost certainly not be possible on an Android device. Instead you'd need to compile Android-compatible class files somewhere else (like on a server, as Yusuf suggests), and then download and load the compiled class files from your app. Or, if you're really looking for a challenge, perhaps you could package a full Java compiler in your app, and compile the Java file(s) that way.
What you are trying to do is something similar to either the JRuby-for-Android project or AIDE both of which builds on device, but only in the case of creating apps to be run on Android as opposed to adding code functionality to an currently running app. While JRuby-for-Android is more for scripting, it may provide enough functionality as it is open source and you may be able to modify it to fit your needs.
The AIDE project appears to be more of an achievement as you can write in Java on device to build an app. Features include Dropbox and git support. AIDE appears to be a closed-source app.
Permissions will be a problem. You can't write files to the partition where the app is installed. Maybe you can if you move the app to the SD card.
An alternative is to create a custom classloader and use that to feed class files to the runtime. I don't know if you can do that on Android.
Is scripting an option? I'm sure you can find a scripting interpreter for Android. Then you can download and execute the script in the interpreter.
So, I've been programming for a while now, but since I haven't worked on many larger, modular projects, I haven't come across this issue before.
I know what a .dll is in C++, and how they are used. But every time I've seen similar things in Java, they've always been packaged with source code. For instance, what would I do if I wanted to give a Java library to someone else, but not expose the source code? Instead of the source, I would just give a library as well as a Javadoc, or something along those lines, with the public methods/functions, to another programmer who could then implement them in their own Java code.
For instance, if I wanted to create a SAX parser that could be "borrowed" by another programmer, but (for some reason--can't think of one in this specific example lol) I don't want to expose my source. Maybe there's a login involved that I don't want exploited--I don't know.
But what would be the Java way of doing this? With C++, .dll files make it much easier, but I have never run into a Java equivalent so far. (I'm pretty new to Java, and a pretty new "real-world" programmer, in general as well)
Java .jar library is the Java equivalent of .dll, and it also has "Jar hell", which is the Java version of "dll hell"
http://en.wikipedia.org/wiki/JAR_(file_format)
Google JAR files.
Edit: Wikipedia sums it up nicely: http://en.wikipedia.org/wiki/JAR_%28file_format%29
Software developers generally use .jar files to distribute Java applications or libraries...
A jar is just a uncompressed zip of your classes. All classes can be easily decompiled and viewed. If you really don't want to share your code, you might want to look at obfuscating your code.
The Java analog to a DLL is the .jar file, which is a zip file containing a bunch of Java .class files and (perhaps) other resources. See Sun's, er, Oracle's documentation.
Java's simple moto 'Write Once, Run anywhere'. create your all java classes as jar file but there are possibilities that still some one can see the Java code by using Decompilers. To prevent someone really looking at your code then Obfuscate the jar using the below link.
Java Obfuscation
You could publish a collection of compiled *.class files.
The most common way to package up Java code is to use a ".jar" file. A .jar file is basically just a .zip file.
To distribute just your compiled code, you'll want to build a .jar that contains your .class files. If you want to additionally distribute the source code, you can include the .java files in a separate area of the .jar.
There are a lot of tools and tutorials out there that explain how to build a .jar.
Technically, you can compile Java bytecode down to native code and create a conventional DLL or shared library using an Ahead-Of-Time compiler.
However, that DLL would need the Java runtime specific to the AOT compiler, and two Java runtimes may not coexist in one process. Also, one would have to employ JNI to make any use of that DLL.
Unfortunately, obfuscation has too many weaknesses...
your tittle doesn't match your comment....
simple have a source jar and a code jar. but, as other people pointed out you can obfuscate the code if you don't want people to read it, it's a pain for other people using your library as they would need the mappings in order to compile and the obfuscator.
A dll is a shared library (from what I read gets instantiated one time across multiple processes)
A jar is a shared library (code gets instantiated per process from the same file)
So to answer your title question there doesn't appear to be one built into java. A library could be made and then supported on all 3 major os's to have a dll equivalent version in java. But, the reason why java made it a new instance per program is for security / sanity reasons. there are custom class loaders, asm and reflection that other programs can modify the classes on load. So if your program does any of these things it could mess up other processes.
You don't have to distribute your source code. You can distribute compiled .class files, which contain human-unreadable bytecode. You can bundle them into .jar files, which are just zip files, and are roughly Java equivalent of native .dll files.
Note taht .class files can be easily decompiled (although decompilers cannot recover 100% of information from sources). To make decompilation more difficult, you can use obfuscator to make sources much less legible.
I'm wondering if a Java library can be called from a VB.net application.
(A Google search turns up lots of shady answers, but nothing definitive)
No, you can't. Unless you are willing to use some "J#" libraries (which is not nearly the same as Java) or IKVM which is a Java implementation that runs on top of .NET, but as their documentation says:
IKVM.OpenJDK.ClassLibrary.dll: compiled version of the Java class libraries derived from the OpenJDK class library with some parts filled in with code from GNU Classpath and IcedTea, plus some additional IKVM.NET specific code.
So it's not the real deal.
I am author of jni4net, open source intraprocess bridge between JVM and CLR. It's build on top of JNI and PInvoke. No C/C++ code needed. I hope it will help you.
You can call Java from .NET if you wrap it in some form to make it accessable and the easiest way is typically to use a Runtime bridge like
http://www.jnbridge.com/
Other way is to wrap your API with java webservices.
check this also http://www.devx.com/interop/Article/19945
Nothing out of the box.
Most java/.net interop that I know uses web services.
If you can create COM components with Java, you can use tlbimp to create an interop assembly for using in VB.Net.
If can create standard DLLs that can be used from C++ with Java, you can write P/Invoke declarations and call them from VB.Net.
If you can create a web service with Java, you can generate proxy class from the WSDL and call it from VB.Net.
In any case, chances are the Java component will live in a separate process. I doubt you can load both the Java VM and the CLR in the same process.
If you have the source code and compile it using the J# compiler, then the answer is yes. If you want to call any pre-Java 2 (aka 1.2) libraries, then these are included pretty much verbatim with J#. More recent stuff is going to be tricky though (i.e., it's not there).
An example where this is used commercially are the yFiles graph layout algorithms from yWorks. These were originally just a Java library, but for the past few years they've been offering a .NET version, which is just the Java version compiled with Visual J#.
It's not without problems, and there are some limitations that you can't get around, but it can be done. So... unfortunately this answer looks pretty shady as well.
You could use JNI to instantiate a virtual machine and then use Java Classes. It will be some fun, though, because you would need to use C++ as a bridge between VB.Net and Java.
This article in java world has a quick tutorial on how to use Java from C++ and viceversa.
http://www.javaworld.com/javaworld/javatips/jw-javatip17.html
If you have the source, Visual Studio will let you convert Java code into c#.