How to access resource files in IntelliJ plugin's jar - java

I am building an IntelliJ idea plugin. I have a component TemplateComponent.java which extends the ApplicationComponent. Here is the link to the code.
I want to read some template files in the TemplateLoader. All the files are placed under root_of_project/templates. When I build the jar and install it in my IDE, I am not able to get access to the files because the plugin is packed as a jar file. However, things work as intended when I am debugging the plugin from within the IDE. Can anyone help me?

Please see com.intellij.util.PathUtil#getJarPathForClass and its usages in IDEA codebase. Unfortunately you might have to add a check if it's a directory (debug) or a jar (production), like in com.intellij.compiler.server.BuildManager#getJpsPluginSystemClassesPath

Related

Where are external libraries in a jar file?

I recently wanted to decompile a jar and edit the contents, so I used jd-gui to decompile it, I saved the project, and I unzipped it. I opened the project in IntelliJ and I was getting a lot of errors for missing types. All the imports that were not built into Java said unknown. Where are these libraries in the jar, and how do I get them out and add them to intellij?
An application may have dependencies that are not included in the jar at all. In that case, the user would be expected to install the dependencies themselves.

How to create jar file in intellij

I am using intellij community version . I am creating a jar file via build artifact but I am getting file size 67357KB . Which is very big .How to create proper jar file .
Thanks in advance
A "proper" JAR file can be very big. So you are probably creating it the right way.
So what you really need to do is to examine the JAR file to see what the build has put into it. If there is stuff in the JAR that shouldn't be there, you then need to look at what your Maven POM file, Gradle file, or whatever to figure out why the build puts the unwanted stuff in there.
If you are not using Maven, Gradle or some other build system, check the artifact configuration that you have created to specify what goes in the JAR, and what is in the directories you have told it to include.

Problem with source of my JAVA Project- Can not upload it to Eclipse

I have a basic but important question.
Our client has an application that has been written with JAVA. We need to modify something in one of the classes.
They passed us the Source files of this project, but I really have doubt that if they sent us the source or no!
This is the structure of zip file:
But when I import it to Eclipse (Import Existing Project to workspace) I see the error that is saying: "No projects are found to import"
How can I be sure if they sent us the source?
Actually I want to be sure and then ask them...
I tried also to open it with Apache Netbeans but it says "No Netbeans projects added".
Can anyone help me about this?
Thanks
Sep
The complete build structure does not look like a default gradle/maven or even Eclipse/Netbeans IDE style (as mentioned already by greg-449)
Howto import plain sources into Eclipse - without maven or gradle
Normally a eclipse project setup looks like
/.project
/.classpath
/src/java
Hello.java
/test/java
HelloTest.java
/bin
Hello.class
HelloTest.class
So I would do following
Create a new Java Project in eclipse and use as customized project location your root folder of your source files.
When asked for source folder location you can either use /src/java (when you
you are free to restructure the files), or add all folders where
sources are located (e.g.maybe nbproject contains sources ?) as source
folders of the project.
Libraries: If the project contains dependencies and you want no compile failures, you must add all libraries to your eclipse project. When you can start the (ant ?) build you will have all libraries inside build folder. Add those to your eclipse project at the build path properties.
After project creation + build in eclipse, the files .project and .classpath are created. Inside the project you should now see at least your sources inside java source folders - and maybe you are able to start the application.
For more information see also
https://help.eclipse.org/2019-12/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2FgettingStarted%2Fqs-3.htm
https://help.eclipse.org/2019-12/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Freference%2Fref-wizard-java-project.htm
Thanks everyone,
I did not found any .JAVA files in the source so I am sure that they did not pass me the complete source file!
Thanks
Sepide

Eclipse is showing me the jar, but I'm unable to use the classes in it

I'm building an existing project with Maven in Eclipse and all the dependencies are downloaded. But some of my classes using some classes from a specific jar are not found : the import statement shows an error.
The strange thing is that when I browse the Maven dependencies in Eclipse, I see the jar. I'm also able to unfold the jar, and browse in the packages, and see the classes in it. So as far as I can say, the jar is here in my classpath, available to my project. But somehow, it's not seen by my code. I've tried rebuilding, from Eclipse and from mvn command line, but I still have the same issue.
Any idea where that issue could come from ?
Thanks
One root cause would be that your jar is corrupted. Not corrupted enough to make it unavailable in Eclipse, but corrupted enough to prevent Eclipse from using the classes inside.
To make sure of this :
go to your local Maven repo, where the jar is supposed to be.
try to open the jar with your zip tool (Winzip or equivalent)
if you get an error saying your jar is corrupted, then all you have to do is delete the directory containing the corrupted jar, and build again your project with Maven : jar file would be downloaded again from your remote Maven repository.
If this time you're able to open it, then probably Eclipse will also, and you'll be able to use the classes inside and your problem will be solved.
If you're still unable to open it, check if the jar is not corrupted on central repository itself.
Had you add the jar file to your Build Path, if not, then you can not find the classes in the jar file.
Try to do this:
In Eclipse:
Right click on the jar file --> Build Path --> Add to Build Path.
Then try again if you can find the classes in the jar file or not.

Project is missing required library [Eclipse]

I have imported a java project into a new eclipse workspace and it gave me a lot of errors:
It says "Project 'myProject' is missing required library: /User/linus/.m2/..." (The directory is longer but not of interest for you)
Now, I have looked inside my Finder to see what is in ".m2" but there is no such directory where it should be. Additionally I let it show all the hidden files but no success. Lastly I tried to get into the folder with the terminal (using cd and then the directory eclipse gave me) but that did not work either.
I saw this post but it did not help me.
I reinstalled Maven to make sure it can be used, this is the output if I type mvn --version and mvn in the terminal:
Does anyone have an idea?
Thanks in advance
EDIT:
I reinstalled Maven and now there is a /.m2 folder. It contains /.m2/repository/ but there is nothing in it...
May I suggest you read a Maven tutorial like http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html
IMHO it's a wonderful build tool because it gets required jar files per the specification in the project object model ("pom") file which your Maven-ized project should have, and you don't have to keep track of jars on your own.
Look in your project for pom.xml, you might even post it here.
To check the sanity of the project and maven on your computer (ignoring Eclipse for a moment), be sure you have a good Internet connection, open a Terminal window, change directory ("cd") over to the project directory, and type "mvn install".
This should trigger the usual Maven build cycle. One of the first things it should do is download all the jars as identified in the pom.xml and store them deeply nested within your /Users/linus/.m2/ directory. Then it should compile all your java classes. Eventually it will probably build a jar file and copy it to somewhere.
If all this works, visit http://eclipse.org/m2e/ for directions to add the Maven plugin (m2e) to your Eclipse. Then your Eclipse should understand how to download jars and build the project per the pom.xml file.
Eventually this all should fix the original "Project is missing required library" problem.

Categories