The magic behind the eclipse jar executable

I can create a jar executable with Eclipse. Let them say that it is called ast.jar , and the main class is ASTExplorer. I can verify that this works with java -jar ast.jar .

Then I unzip the jar file into a separate directory and verify that ASTExplorer is in the astexplorer directory. But when I java -cp . astexplorer.ASTExplorer this java -cp . astexplorer.ASTExplorer java -cp . astexplorer.ASTExplorer , I get this error.

 java -cp . astexplorer/ASTExplorer Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/swt/widgets/Composite at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631) at java.lang.ClassLoader.defineClass(ClassLoader.java:615) 

The fact is that there is no org / eclipse / swt directory in the jar file.

Directory screenshot

What is the magic behind the jar executable? Why doesn’t it start when unpacking?

+6
source share
1 answer

When Eclipse packs a runnable jar, it includes dependencies as jar attachments, and also includes a special class loader that understands how to find classes in nested jars. It works only with nested banks, and when you extract everything that you interfere with its work.

I looked into the released Eclipse runnable jar, and here is what I think you will need to do:

  • Extract everything to the directory. Dependency bans are retrieved in the top-level directory
  • Delete the directory org / eclipse / jdt / internal
  • Run the main class from the top level directory with

    java -cp .;dep-jar;dep-jar;... your.main.class

I tried this with a running jar, in which I turned out to be lying, and it works. Step 2 is not absolutely necessary.

+2
source

Source: https://habr.com/ru/post/926151/


All Articles