How to display the source code of an external JAR in Eclipse? - eclipse

In Eclipse, I just imported an external JAR. Viewing any of the classes in the Package Explorer will instead of showing a source code open a Class File Editor with saying "Source not found". The folder of the JAR I have downloaded, however, has only JAR, no lib, no src, no docs.
Is there still a way how to view/generate a view the source code so then I can view it in Eclipse properly?

.jar files don't contain source code but are more like binary files for Java.
You can either get the source code from the projects page (if it is an OpenSource project of course)
An other possible way to view the source code of a .jar file is by using a decompiler (http://jd.benow.ca/; Also has a Eclipse plugin I think). This method can be very painful though when an obfuscator has been used by the developer who generated the .jar file.

Try this :
Download java decompiler from http://jd.benow.ca/ and now double
click on jd-gui and click on open file.
Then open .jar file from that folder.
Now you get class files and save all these class files (click on file
then click "save all sources" in jd-gui) by src name.

You cannot view the source form a .jar files as it contains binaries.Use a java decompiler instead to decompile the .class files and view their sources.

If possible I suggest you to use Maven for manage dependencies of your project, in most cases it did the trick for you.
See: Get source JARs from Maven repository

Related

Eclipse - why cant it integrate jars in lib folder into project?

I manually copy needed jar files into my project lib folder. Then, I try to add them to build path via "configure build path option". If i choose "add jar", then the lib folder does not show up in the "to choose from" list. So, I am forced to use "add external jars" option instead. But, that option does not make the jars a part of the project even though they lie in the lib folder. After I restart eclipse 2-3 times, the jars are magically integrated into my project.
Why is eclipse behaving this way ? Why can't I do this easily ?
Please help me.
By default, Eclipse dose not detect file change which come from outside, like copy a jar file in lib folder manually.
So after you copied jar files, you have to refresh the project by pressing F5 while selecting your lib folder. I believe that's why eclipse cannot found your jar files.
If you want to solve this problem once for all, you can active "Refresh using native hooks or polling" via
Window -> Preference -> General -> Workspace
However, this might slow down Eclipse if you have a big project with many many files.
UPDATE
As Bananeweizen mentioned, instead of doing all the copied from outside of Eclipse, you can also copied those file into Eclipse, Package Explorer View for example. This way Eclipse will detect and refresh folder automatically.
The way I was taught to add external jars to an eclipse project is to drag the jar file from explorer into the lib folder in eclipse and then on the dialogue eclipse responds with is choose the copy to option and eclipse imports the jar file and copies it to the lib location at the same time.

Eclipse : How to remove the attached Source which was added to a Jar

I am using Eclipse IDE (Helios Version).
As part of the build path, I have a jar file for this Application, I have attached the source code for this jar file using Attach Source Option.
Please tell me how can I remove this attached source for the jar file?
Open the .classpath file and delete the source attachment part.
Or in the Build Path control panel, find the jar file and remove the source attachment (it is one of the detailed options there).
If you have two different projects in eclipse that are linked, the best way is to open both of them in eclipse workspace. If you do that, you don't need to "Attach Source..."
If you added the second project as a Source Attachment by mistake, you could try to remove the projects from eclipse workspace and adding them again. That worked for me.

How can I browse the source code in a jar file containing source code in Eclipse?

I have a jar file that contains source code. How can I browse this source code in Eclipse?
At the moment, my Eclipse opens the jar file in a Text Editor, and it isn't that helpful.
When I look in the preferences "General" > "Editors" > "File Associations" I have no associations for *.jar and if I look in the available "Editors" I can't find anyone that seem to be a good fit for jar files.
How can I browse the source code in a jar file using Eclipse? Is there any Editor I should configure for jar files?
If you have the jar added to a project's classpath, you can view its source by:
Expanding Referenced Libraries in the Package Explorer view
Expand the jar you're looking at
Double clicking on a .class file
You need both the .class and .java file in the jar to view source, though.
If you have only the .java file, the class won't appear at all.
If you have only the .class file, the class will appear as bytecode.
Example:
Extract the jar in some source directory (it's a zip file), and create a project with this directory as source.
Or, if this source jar file contains the sources of a classes jar file that is used as a library in your Eclipse project, just right-click on this jar file, select "Properties", then "Java Source Attachment", then "External File", and select the source jar file. Then each time you browse a class from this classes jar file, Eclipse will automatically show you its source code.
If you want just to open jar o war files (and search or browse scripting code), install the Eclipse plugin called "Eclipse zip editor", from eclipse marketplace. Just in case it's the website to drag and drop to eclipse

Import Libraries in Eclipse?

I just recently downloaded the dom4j library, but for the life of me I have no idea how to access it. I dropped it in the plug-ins folder and rebooted Eclipse, without success. For some reason finding a straight answer for this is more difficult that I thought it would be.
No, don't do it that way.
From your Eclipse workspace, right click your project on the left pane -> Properties -> Java Build Path -> Add Jars -> add your jars here.
Tadaa!! :)
Extract the jar, and put it somewhere in your Java project (usually under a "lib" subdirectory).
Right click the project, open its preferences, go for Java build path, and then the Libraries tab. You can add the library there with "add a jar".
If your jar is not open source, you may want to store it elsewhere and connect to it as an external jar.
For the Android library projects, I do it as in the attached screenshot:
Right click the project, select Properties->Android and in the library section click Add. From here you can select the available libraries.
If you are importing a jar file, then importing them as jar or external jar, as other posters posted would work. I prefer to copy/paste jar file in the libs folder (create one if it doesn't exist) and then import as jar.
If you want to get this library into your library and use it, follow these steps:
You can create a new folder within Eclipse by right-clicking on your project, and selecting New Folder. The library folder is traditionally called lib.
Drag and drop your jar folder into the new lib folder, and when prompted select Copy Files.
Selecting the Project tab at the top of the screen, and click Properties.
Select Java Build Path followed by the Libraries tab.
Click the Add JARs… button and select your JAR file from within the lib folder.
Your JAR file will now appear in both the lib and Referenced Libraries folders. You can explore the JAR's resources by clicking Referenced Libraries.

Eclipse plugin that shows the META-INF of a jar library

Currently in eclipse when you open a library jar, it shows all the packages and classes, but if you want to see the META-INF content (e.g. manifest.mf), you have to open the file externally (e.g. using winrar)
Is there a simple way buit-in in eclipse to do so, or a plugin that does this?
you can use the Project Explorer view (part of Eclipse Java EE) instead of the Package Explorer view, and view everything inside the jar, including properties files, xmls, etc.
Jar-Plug will do the trick
http://jar-plug.sourceforge.net/
Under Linux, I simply associate (inside Eclipse) the jar extension with the file-roller application, which lets you browse the innards of packed files. Works fine for me. This doesn't directly display META-INF, of course, but I can go there and click on the file (e.g. manifest.mf). This is very convenient.
Under Windows, I suppose you could similarly link out to something like 7Zip. I have no experience to contribute there, alas.