Java - How to make program run jar files internally - java

I was wondering ... I want to use a plugin-type thing with my Java program.
Here is the situation:
I have compiled a source file (.java) into a .jar file using MY .JAR program as a library. How to I make MY program run the other .jar file internally (using the main program as a reference).
I know this is weird (it sounds weird to me too), but if anyone understands what I am trying to say, please comment.
Thank you all in advance!

OK, here's a draft of how to do it.
Create an interface with a "run()" method.
Your .java plugin must implement that interface.
Load the all classes in classpath (help here Find Java classes implementing an interface)
run your plugin by executing the run method of the interface.

You would have to run the jar using the standard syntax. Your question is basically about running console commands inside java. Here is a nice answer to a similar question:
link!
This isn't necessarily exactly what you want, but it's goal is to put you on the right track, basically you would get the jar placement, then check for the system, then use the technique used there to run a command through the specific platform's console.

Related

How to make VS Code recognize .class file?

I'm trying to use VS Code with java, but I have an issue.
As a student, my teacher gives me a file named Clavier.class.
I have already wrote a programm with another IDE, and when I execute it in the command of Windows, it works without any problem. But, when I put this programm in VS Code, it doesn't recognize Clavier.saisirInt, which is a static method in Clavier.class. I guess that VS Code can't use a class which is in a File.class, but is there a way to make it works ?
Here is an image of my workspace and you can see the error at the bottom
I assume your lib/ folder is placed onto the execution classpath. If this is the case, that's where compiled class files should go, not next to your sources
Otherwise, you'll need to inspect the java command that actually runs your code to see what/if the -cp argument is set to, or include those class files similar to how the documentation shows for JAR files (which are just zipped packages of class files, and honestly what the teacher should provide instead)
By default, VS Code will reference all JAR files in workspace's lib directory using the glob pattern lib/**/*.jar
And so, you need to adjust this to include lib/*.class after moving your files there

Can we run decompiled java (jar to java)

I have decompiled a jar file. Can we run this file using eclipse or some other IDE. Or any way to do this. Thanks in Advance.
It depends on a number of things, such as:
whether the original JAR was runnable,
whether the decompiler produced compilable Java code,
whether the decompiler produced correct Java code, and
whether you have all of the dependencies for the decompiled code.
Note that decompilers tend to have difficulty with code that has been obfuscated, and code that uses the latest Java language features.
It depends on the decompiler used: some do produce compileable code, some don't. If your decompiler's output can be recompiled you could include it in an eclipse-project and run the main-class (if there is any).
Make sure to have all other dependencies of the jar-file in the build-path.
Yes you can If it contains main method in any of it's classes.
Yes you can run the decompiled jar file. Right click on the class which contains main method and select Run as Java application. If required, adjust the classpath (You cand do this in eclipse using java build path and debug settings)

Converting dll to jar

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

build multiple shared libraries

Hi I have a java program which has to invoke a native program, and this native program are given by two so files. So I create my so file in order to use this native program APIs to do something for my java program. I was trying to merge two so files with my created so file into single one, and run my java program. However, it seems that it failed this way. To be more concrete, here is my example.
I have a java program A which has to invoke some native code. Therefore I've written some native code and built it as a shared library (called: C.so).
Unfortunately, the native code I've written have to use other code which is in other so files. (A.so, B.so)
Thus, any ideas how to compile my so file with A.so and B.so in order to make my java program work?
I'm assuming the following:
When you link c.so, you are listing a.so and b.so on the command line.
When you run ldd on c.so, you see a.so and b.so.
When you run, you set -Djava.library.path to include the directory containing all three.
When you run, you do NOT set LD_LIBRARY_PATH to include the directory containing all three.
You will get the desired results if you set the LD_LIBRARY_PATH environment variable to include the directory with the libraries in it.
For more explanation, and an alternative, see https://github.com/bimargulies/jni-origin-testbed.

Alternative to Eclipse's Abstract Syntax Tree parser for code manipulation

Background
I am writing a program that will do some bulk renaming of members and functions in a directory of java source code to de-obfuscate the code based on a look-up table .csv file passed in to the program.
What this is for is the source code I have was written against a obfuscated jar. I have a de-obfuscated version of the jar that was run through a customized version RetroGaurd and I would like to parse the mapping file that was passed in to RetroGaurd to de-obfuscate the function calls my source code makes in to the jar. If I just compile my code and run it through RetroGaurd too when I decompile I loose all of my nice commenting and formatting (unless there is a option on RetroGaurd that I missed).
Problem
I found the Abstract Syntax Tree parser built in to Eclipse and it looks perfect for my uses, however I am not planning on writing my program as a plugin for Eclipse, this is going to be a stand alone jar that can be run on any machine.
My main concern is as I write my code I am getting a lot of dependencies on internal jars that Eclipse uses. I know that if I conform to the EPL for the library jars I will have no issues distributing it, but I am concerned about this project getting bigger and bigger as I write it as more and more jars from Eclipse's SDK are required.
Are there any other projects out there that would give me the ability to parse Java source code to do find and replace reliably like AST will allow me to, or is there a way to use RetroGaurd (or a program like it) to run the same de-obfuscation but keep my comments and functions the same without needing to run the de-obfuscated program though a de-compiler afterwards?
If you're worried about the need to run Eclipse GUI to execute your code, then you could consider running the plugin in headless mode. This would allow your plugin to be run from command line. See this SO thread.
You could also use any other open source java compiler. For e.g., openjdk's Java compiler. Please refer to the Resources section.
Hope it helps.

Categories

Resources