Java compiled code to source

Java Compiler API — Compile Code to a File and Load it using Default Class Loader

In the previous topic we saw how to compile java sources from files. In this example we are going to show how to compile a string object containing java code. Here are the steps:

  1. Extend SimpleJavaFileObject , let’s say as class JavaStringObject
  2. Override getCharContent(..) in JavaStringObject to return our in memory string object instead of reading some file content from disk.
public class JavaStringObject extends SimpleJavaFileObject < private final String source; protected JavaStringObject(String name, String source) < super(URI.create("string:///" + name.replaceAll("\\.", "/") + Kind.SOURCE.extension), Kind.SOURCE); this.source = source; > @Override public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException < return source; > >
public class StringCompilation < public static void main(String[] args) < JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); DiagnosticCollector diagnostics = new DiagnosticCollector<>(); JavaCompiler.CompilationTask task = compiler.getTask(null, null, diagnostics, null, null, getCompilationUnits()); if (!task.call()) < diagnostics.getDiagnostics().forEach(System.out::println); > > public static Iterableextends JavaFileObject> getCompilationUnits() < JavaStringObject stringObject = new JavaStringObject("Test", getSource()); return Arrays.asList(stringObject); > public static String getSource() < return "public class Test + "public void doSomething() + "System.out.println(\"testing\");>>"; > >

Loading the Compiled Source

Classes compiled this way can only be loaded (via reflection) if they are already in the classpath. The compiled file, in our example, goes to the project root if we are running it from an IDE. I’m using Intellij:

That means we have to set the root of the project as classpath before running the main method. There’s currently no standard way to set classpath during runtime.

Example Project

Here’s the complete example project. Notice I implemented a predefined interface ITest to our generated class Test so that we won’t be doing everything (method calling) via reflection.

Dependencies and Technologies Used:

Источник

How to Extract the Source Code of a Java Application ☕

How to Extract the Source Code of a Java Application ☕

If you write Java applications, you may already know that, to be able to easily use your application on another computer, you need to compile its source code. With Gradle, one of the commands that do this is gradle assemble . Once compiled, an executable Jar file is created (like APPLICATION.jar ).

If you open this file, you will see that the file is incomprehensible to you, unless you are a robot. 🤖

Unfortunately, if you think your source code is completely opaque, you are mistaken. 😟 Effectively, the Jar file is in reality a simple Zip file with another extension. 🤡

If you rename the file as APPLICATION.zip and extract the archive, you should see a few folders there, which contain a lot of interesting files.

  • the framework and its version: Spring-Boot-Version: 2.4.4
  • the list of dependencies along with their respective version
  • and the structure of your application with its files as *.class format

Ouch!, you weren’t expecting to see these files, right? At least, they’re still unusable, for now.

If you look at the same file with your IDE, some of them, like Intellij IDEA, can even decompile this type of file for you! 😮

If you look at the following images, you will see that there is not much difference between the source code and its decompiled version. All comments have been removed along with some final keywords. Do you see any other differences? 🤨

Even if you don’t have an IDE that can decompile a class file for you, other solutions allow you to achieve this goal. I tried one of them in the second part of this post, but the results seem to vary from project to project.

So, if you really want to protect your intellectual property, I suggest you choose another language and above all, host your application yourself. 🙃

Procedure

Warning: decompiling, or reverse engineering, a part of an application seems to fall into gray areas, depending of the jurisdiction.

I suggest you to do this only it if it is your application or if you have the author’s permission. Otherwise, you should only do this if it is necessary to ensure software interoperability.

You can check out the additional links at the end of the post for more information on the topic.

Install JD-core-java

# Clone the repository git clone https://github.com/orangecms/jd-core-java.git # Build the application cd jd-core-java && ./gradlew assemble && cd .. # Move the JAR file to the current directory mv jd-core-java/build/libs/jd-core-java-1.2.jar jd-core-java-1.2.jar # Delete the directory containing the application source code rm -rf jd-core-java

Decompile the JAR files

# Copy the JAR files to decompile to that directory cp ../johnDoeProject/*.jar . # Iterate over all JAR files for JAR in *.jar; do # Get the filename FOLDER="$" # Decompile the JAR file into its proper directory java -jar jd-core-java-1.2.jar $JAR $FOLDER done # Remove decompiled JAR files rm -rf *.jar

Источник

Читайте также:  Php date format gmt
Оцените статью