Creating java executable using JNI? - java

I am trying to create executable under windows platform for Java program using JNI ,C/C++ and invocation API, I have already created jar file for my program which includes all dependencies. I want to embed it in exe file, I was successful in running simple main class(present in file system) using JNI invocation API, I am planning to add jar file as resource in C/C++ program. But I don't know how do I run that jar file , One option is create temporary jar file on file system and run it using java, But I do not want to expose my jar file to everyone for security reasons, How can I run jar file on the fly using JNI ?

Compiling Java to an executable with GCJ does not work all the time, there are limitations as far as using reflection and other items such as UI classes, Look at this page.
If you convert you Java Code to a library or simply another module then you could link to it and simply run it without the need for a JVM.

My initial reaction was that I would be shocked if you could get this to work and have it be performant. But then I started thinking about it, and maybe you could pull this off using a custom class loader. If you embed the jar in the exe as a resource, it would be exactly the same as having the jar bytes be present at a particular offset in any file (whether an exe or not).
So, here's a potential strategy: implement a custom class loader that accepts the exe path and offset of the jar resource in that file. This would use a custom version of ZipFile that uses a fixed index offset for it's reads (unfortunately, it isn't going to be possible to use ZipFile itself - but if you grab the source of ZipFile it should be pretty obvious where you'll need to add the offset).
There is a bootstrapping issue here (how do you load the custom class loader?) - but I think it might be possible to do that from the JNI side. Basically you'd store the .class file for the loader as a separate resource in the exe, load it fully into memory then construct it using JNI calls. That will be a hassle, but it's just for one class, and then you can let the Java runtime take over the rest.
Sounds like an interesting project (Although, as others are pointing out, there isn't much security in what you are doing... I suppose that you could encrypt the embedded jar and add decryption code to the classloader, but you've kinda got to decide how far you want to take this thing).

Related

load function arguement Jar during runtime

I am quite new to working with jars and from reading online I think what I am describing is possible but I am not sure about it. If anyone has an alternative pipeline, I welcome that as well. Thanks for your time.
I would like to do the following:
I have a java program running on my machine and I would like to accept requests jars during runtime from a remote user. The users submits the jar and I would like to add the classes in the jar (which implement some predefined interfaces) to my classpath and call functions that are implemented in the submitted jar. I am using thrift as my communication platform.
How can I receive the jar file in thrift?
Once I have the jar, how do I load it? (There is no path etc because it was received from elsewhere?)

JavaWebStart- multiple jars in same jnlp?

I'm trying to convert my java software jar into a Java WebStart application. I've managed to successfully deploy it as a WebStart with a jnlp file that provides details about the software's jar.
However, my software requires certain details to be stored locally in a file on user's machine. These details can be set & the file can be generated by having the user run another java software (a very small java utility software) coded by me. I wonder if it's possible to package in this smaller software's jar as well along with the main software using the tag. As far as I know, we can use the tag to specify additional jars/ libraries that our software jar uses.
Can we package in another java jar created by us in the jnlp and would it then get downloaded alongside the main software jar and other libraries (jars) when the user launches the jnlp ?
Kindly let me know. Thanks.
P.S: I know that all jars downloaded by a WebStart application end up in the deployment cache. Is there no way to specify where they should get downloaded to ? In my case, I would prefer it if the 2nd software's jar gets downloaded in an easier-to-find location.
Yes, it would be downloaded - but it would end up in the deployment-cache with no easy way to find it (names get all mixed up) or start it.
There's no way around webstart keeping the files in the deployment-cache: if you could specify the download-location different versions of jar-files with the same names and different codebase could end up in the same location causing no end of trouble.
I see two ways to get the wanted functionality:
Add the ability to change the local configuration as an additional function in the main application. You probably can use most of the existing code but use a method-call instead of starting it as a separate application
Create a second jnlp-file to start the configuration-program. From what you write it should be pretty small and not too much effort to create.

behavior of external executable

I am currently writing a program in JAVA that examines the behavior of external executable. One of the requirements is to observe the file operations of the external executable in real time (check if the executable creates/ deletes/modifies any file). I tried to find a suitable API in java to help me do this though it was not possible to find one. I have found the Class FileAlterationObserver which is not suitable for my program since you have to specify manually all the directories you want to monitor.
I was wondering if any of you knows a good API to use?
Thanks for your time in advance.
Without java, you could use the linux lsof command to list the open files in the system. Alternatively, and with Java, you can use libnotify, but you will need to specify the folders. I can't see any other way of doing this with pure java.
EDIT #Keppil linked you to the file change notification API that looks way more suitable than libjnotify. I wasn't aware it existed!

Shipping Java code with data baked into the .jar

I need to ship some Java code that has an associated set of data. It's a simulator for a device, and I want to be able to include all of the data used for the simulated records in the one .JAR file. In this case, each simulated record contains four fields (calling party, called party, start of call, call duration).
What's the best way to do that? I've gone down the path of generating the data as Java statements, but IntelliJ doesn't seem particularly happy dealing with a 100,000 line Java source file!
Is there a smarter way to do this?
In the C#/.NET world I'd create the data as a separate file, embed it in the assembly as a resource, and then use reflection to pull that out at runtime and access it. I'm unsure of what the appropriate analogy is in the Java world.
FWIW, Java 1.6, shipping for Solaris.
It is perfectly OK to include static resource files in the JAR. This is commonly done with properties files. You can access the resource with the following:
Class.getResourceAsStream ("/some/pkg/resource.properties");
Where / is relative to the root of the classpath.
This article deals with the subject Smartly load your properties.
Sure, just include them in your jar and do
InputStream is = this.getClass().getClassLoader().getResourceAsStream("file.name");
If you put them under some folders, like "data" then just do
InputStream is = this.getClass().getClassLoader().getResourceAsStream("data/file.name");

Any way to get a File object from a JAR

I have a JAR file that contains an API that uses external model files. I would like to include the model files in the JAR itself so it easier to use for other developers. The API will accept a File object only, is there any way to do this? I have already tried the following, and they have failed:
Using class.getResourceAsStream(). This would work if the API accepted an InputStream.
Parsing the classpath and trying to build from the entries (the JAR will show as app.jar)
I suppose an option is to use getResourceAsStream and move the files to a permanent location on the HDD but, I do not like this option. There has to be something better, any thoughts?
Resources in a .jar file are not files in the sense that the OS can access them directly via normal file access APIs.
And since java.io.File represents exactly that kind of file (i.e. a thing that looks like a file to the OS), it can't be used to refer to anything in a .jar file.
A possible workaround is to extract the resource to a temporary file and refer to that with a File.
Note that generally APIs that try to handle files should be written to handle InputStream/OutputStream as well to allow this kind of operations to suceed.

Categories