Error: Unable to access jarfile IDEA 2018.2 and JDK 10 on Ubuntu

Answered

Hello

I am trying to learn IDEA, migrating from Eclipse (right now I wonder why??).

Something that is trivial to do in Eclipse seems to be crazy complex on IDEA.

Building a Jar file to call another jar file (console command shell app).

I have a JAR file I am building, call it abc.jar and from there I want to use another JAR file class, call it xyz.jar.

I want to package abc.jar to contain xyz.jar so that I can simply run it as java -jar abc.jar.

This is trivial to do in Eclipse.

In IDEA it is far from it.

The code compiles OK 100% but if I try to Run it or Debug it then I get:

Error: Unable to access jarfile

Here is the command IDEA generates:

/home/yuri/Downloads/jdk-10.0.2_linux-x64_bin/jdk-10.0.2/bin/java -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:33985,suspend=y,server=n -javaagent:/home/yuri/.local/share/JetBrains/Toolbox/apps/IDEA-C/ch-0/182.3684.101/lib/rt/debugger-agent.jar=file:/tmp/capture165.props -Dfile.encoding=UTF-8 -classpath /home/yuri/.local/share/JetBrains/Toolbox/apps/IDEA-C/ch-0/182.3684.101/lib/idea_rt.jar -jar ""

Error: Unable to access jarfile

Process finished with exit code 1

---

I dont understand how this can happen given code compiles correctly, i.e. global library dependency is set correctly and I am using classes from the dependent jar file, else the compiler would have barked at me.

I tried adding module dependencies, same error. 

Please help, else I will simply stay on Eclipse, which just works.

I am using IDEA 2018.2 Community edition, I am not sure if this version does not support what I am trying to do?

thank you

 

 

0
5 comments
Avatar
Permanently deleted user

sorry, I managed to debug it now but if I build a JAR for the main console app it fails to run in bash shell because the dependent JAR is not included inside main JAR so I get class-not-found-error.

I can not see how to package all dependent jar files into the main jar console app....

I can run it successfully as java -cp abc.jar:xyz.jar MyClass but I wonder if I can package it all into single jar file and use java -jar myjarfile.jar ??

 

 

0

>I have a JAR file I am building, call it abc.jar and from there I want to use another JAR file class, call it xyz.jar

If you add xyz.jar jar to the module from which you build the abc.jar - IDE will include it into the artifact when you create one.

Generate the jar selecting the copy to the output directory and link via manifest option in the dialog and for the Directory for META-INF/MANIFEST.MF: select the directory when yuo want IDE to create the manifest file and specify your main class in the Main Class field. After selecting OK, IDE will create the META-INF/MANIFEST.MF in selected directory where it specifies the main class and the class path.

1
Avatar
Permanently deleted user

thank you Andrey.

Some progress - what happens now is when I build artifacts that both abc.jar and the dependent xyz.jar are created in same directory in Linux, in the out directory. There is still 2 jars created. I can run the main jar using java -jar abc.jar and it runs OK but xyz.jar is separate file from it.

What I want is a single one jar file with all its dependent jars packaged/stored inside the jar. This is so that to deploy it in production I only need to copy 1 jar file - to be run via java -jar command.

This is why I do on Eclipse with near zero troubles.

Can I do the same with IDEA Community ??

Instead of selecting "copy to output directory and link via manifest" should I pick the default radio-button "extract output to target jar" (I forget the correct wording of the first of two radio buttons) ??

Thank you for your reply!

 

0

>Instead of selecting "copy to output directory and link via manifest" should I pick the default radio-button "extract output to target jar" (I forget the correct wording of the first of two radio buttons) ??

Yes, this option will extract all jars (dependencies) into a single jar file. Make sure the manifest file is correctly specified.

There is no "create runnable jar" feature yet in IntelliJ IDEA. Please vote for https://youtrack.jetbrains.com/issue/IDEA-150485.

1
Avatar
Permanently deleted user

thank you, voted +1 

 

0

Please sign in to leave a comment.