Converting dll to jar - java

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

Related

Java calling an external process - exe

I am building a java application, which at some point utilizes an external exe. At this point, I'm trying to simply add this exe as some sort of library, which I can use in process call, so user wouldn't have to install it..
This exe file is an command line tool which produces some output, which is further processsed by the application.
So my question is, how does one include exe file within a java application, instead of calling it as a system process. Also acceptable would be, if this exe would be for example in the final lib folder, where java app would fetch it and execute it.
I hope it is clear and thanks for any help.
Java interacts with other (native) code in a couple of ways:
to specifically coded libraries through Java Native Interface (JNI)
to external tools by forking a child process using Runtime.getRuntime().exec(...)
through network communications
In the first case, it is useful to include your JNI library with you Java program. It's thinkable to include it as a class resource in your JAR file, but that would be against the ideas of JAR files (holding classes and resources). More likely you should bundle the library (for all target platforms) with some sort of installation package (e.g. InstallAnywhere or others)
In the second case it's not useful to include the binary into your Java program (i.e. in the JAR file). Rather it most likely has (should have) its own installation procedure and your Java code should use an appropriate commandline (PATH) to find the executable.
I think the third case is not relevant.

Questions about .jar files and running a java program through PHP

I'm building a website which will use files in order to render social graphs. These files are created by a backend in JAVA. This JAVA program consists of 4 classes and 4 libraries. I know that in order to run a JAVA program through PHP, I need to call it with the "exec" command but all the examples I saw have .jar executables and not .java files in the syntax. So, I'd like to ask two questions:
Is it possible to call just the .java main class from PHP without creating a .jar file? And if so, what's the syntax of the "exec" command?
If I have to create the .jar executable, would I just have to place it in the website folder and just call it? I mean, does the .jar file contain all the classes and libraries that the program needs? I don't talk about JAVA libraries, I have 4 specific libraries (MongoDB, Neo4j, GEXF parser and Lucene DB) which are not part of Java defaults. Thanks in advance and please forgive my syntax and spelling mistakes.
You have to at a minimum compile the .java files into .class files. It would be easier if they are in a jar file, but that is not required.
1)
a) java -cp /location/to/.class/files
b) java -jar /location/to/the.jar
2) the jar file does not have to be executable, though if that makes it easier, set the manifest up correctly and there you go.
When you say libraries, you mean other .jar files or native .dll or .so libraries? If the later, you have to have your library path setup to find them.
There is a PHP-Java bridge project on SourceForge. Also, you could expose your back end process by running it as a servlet, which is probably the proper way of doing it. If all else fails you can just "exec" it.

Linking Java with C++ statically using JNI

Is it possible to link a C++ library to a Java program statically, in a way that will make them into a single file ,just like linking 2 C++ libraries?
(I read that java programs can also be compiled to EXE).
Theoretically this should be possible to create one EXE that already includes the required JNI functions used by the JVM.
This EXE would have to load the Java part by starting a JVM instance in the same process (by loading jvm.dll and executing it as shown in question JNI Java in c++).
The Java-EXE-wrapper I know do not support something like this as they come with a pre-compiled EXE that gets the used JAR attached as resource. Therefore I assume you would have to build you own C/C++ executable and implement all the functionality you need.
When I use JNI I include the dll with JNI support into my jar file. Then access it by classpath. You will have single jar file.
It isn't possible unless you have access to a static version of the jvm.lib library. It is distributed as a dynamic-link library referring to jvm.dll. You can't do this.

Java Makefile Output Single Executable

How can I create a Makefile to compile my Java program with a single source file located in src/Hello.java to ouput a file that I can run with ./prog
(1) Take a look at http://ant.apache.org/ - that's a build tool suited to Java better than make. For example check out the javac command in ant.
(2) You have to run java programs by running the java virtual machine (java) and by telling it what to execute. There is no ./xxx way to run a program in java; that method executes scripts or executable programs, and a compiled java program is bytecode which is neither. What you need is to create a little shell script, call it "Hello", give it execute permissions with chmod, its contents should be something like:
#!/bin/bash
java -cp . Hello
Make is not a great tool for building Java programs, primarily because its core design is to identify out-of-date sources (either source or object files) and apply transforms to them. The Java compiler handles this dependency checking already, and defining a Make rule for Java compilation is more trouble than it's worth.
Adrian Smith has already suggested Ant. I'm going to suggest Maven as an alternative.
My main reason for this suggestion is that it's very easy to get a simple project going in Maven: you can tell Maven to create the entire directory structure, and then it just works. And although you may not like the directory structure that Maven creates, there's a benefit to the consistency if you're working in a large project. Maven does have its limitations, and some of them are extremely painful, but you generally won't run into them until you have the knowledge to work around them.
As for running a Java program, you need to invoke the JVM somewhere along the line. A shell script is one approach, but generally it's easier just to invoke the JVM directly. If you create an "executable JAR" (which Maven will do for you, including options to include all dependencies), you can invoke it like this:
java -jar executable.jar

Is it possible to run an executable jar file on a machine without installing java first?

Can I include the rt.jar in my executable jar file and double click to run it without installing java on the machine first ? I hope it to use that rt.jar in my jar to start it self, possible ? If not, any other way ?
No, you need a java virtual machine. rt.jar is also interpreted by the virtual machine and is just the java class library.
If you're looking to turn the code native you might consider gcj which can convert java code to machine code and wrap it up in an exe as per gcc. However, I'm not sure what version of java gcj supports - I've read somewhere it isn't very recent.
It looks like Launch4j can include a bundled JRE, so you might consider this.
Java needs a Java virtual machine (JVM/JRE whatever you want to call it) to run java applications. However, I'm not so sure you need Java to be installed, rather it just be present.
You could provide this alongside your JAR (with a batch file to run the JAR files - as there won't be associations if it's not installed) but it would seriously expand the size of your project.
There are licensing issues to address too.
The file rt.jar has Java classes in it (in the form of Java bytecode). In order to interpret the classes, you will need to have Java installed.

Categories

Resources