adding source code to build path? - java

I downloaded an API but there is no jar file in it. It has the packages(folders) and the source code. I tried adding this in various ways in Eclipse but still I don't get the "import ...." when I press F2 on the underlined red classname. I have added projects and Jars in Eclipse all the time and it has worked. Since this is the source code should I take any extra steps in order to make it work?
Regards!

If the source is available to you (.java files) you must create a new source folder, then copy the source tree (root package e.g. the folder com or org) into the new source folder just by using a standard file explorer and copy&paste:
Edit: You could add the source files to your existing source folder as well, but I'd avoid that. Best solution would be to create a separate Java project containing just the source files of the external library. You also need proper code management, so that you do not accidentially make changes to the external source files!

You may create jar from the classes you have received in API packaging and place that jar into the classpath of the project
Enjoy !!!

Related

(file) not found in build path

I downloaded and decompiled a jar file for a plugin for minecraft, and I added it to eclipse by creating a new java project, Import > archive file > [decompiled jar.zip]
That results in this:
After that, I changed a single line of code, then I tryed to export it.
However, when I attempt to export it, I get this message:
I have no idea what I'm supposed to do to fix this. Please help.
de (and not only its content) and the other folders as well must be included in the source folder.
Try to move all the folders (except META-INF) and files to src and then create your jar.
(Note that there would be a warning if you didn't exclude META-INF because it is generated you would override it but it wouldn't prevent generating the jar).
NOTE
I don't know what you will do later with this but you'd better use Maven, Gradle (or even Ant) to manage you future jar builds.

Create project from existing project Netbeans

I have downloaded some source code that contains src folders with some .java files. I want to try to run the code in netbeans.
This is the structure of the folder :
C4.5/src/main/java/myc45/
and in these folder include some .java files.
What should I do first?
When you create a project in Netbeans, one of the options in the project creation window is create project from existing source. If you have an existing project, you can also edit the project properties and tell it what the source folders are.
As an alternative to #PaulJAbernathy 's solution:
create a new project in Netbeans
via the projects windows, create a package myc45 (the package name used in the code you want to import) - you can do so by rightclicking, new Package
now, inside the src directory of your project directory you'll find a directory called myc45. Drop the source files into that directory using whatever file browser you commonly use. You'll see that Netbeans picks up the files almost immediately in the project explorer.
you can now use the code.
A bit messy, but there are advantages: if eg you want to transform a bunch of existing code files into a Maven type project, this is probably the easiest way.

Why don't I see code when viewing included Jar libraries in Eclipse?

There doesn't seem to be any code there. I expected to see class declarations so I could see what the code does but instead there's some
Are they somehow precompiled? What's the difference between included Jar file and a pure code?
You are looking at .class file, which is a generated when you compile .java file. To see what the program does, you have to look into .java file. You can refer official java documentation for that.
A .jar file is packaged file with .jar extension, it contains compiled java files and their class files. This file is usually imported into projects to use the classes defined in that package.
You can use "jar xf jar-file" command in command-prompt/terminal to extract the files from jar and look into the package.
A JAR will normally contain compiled class files. It may also contain source files or there may be a separate JAR that contains the source files, but not necessarily so.
If you want to use the library in your project, then a JAR of compiled class files is what you want. If you want the source code, then you'll have to see if it is available from wherever you downloaded this from. If all you want is to see how to use the classes, then probably what you want are JavaDocs for the library you are using. This is an HTML based API documentation.
Well, this is because you haven't attached any source for the mentioned dnsns.jar. You can attach source to existing JAR files in Eclipse. Refer this SO post: Is there an easy way to attach source in Eclipse?
For this specific dnsns.jar, it is part of your JRE, and if you are not able to see its source in your IDE, then it means that the Java that you have setup in IDE lacks the source. If your installation does not have the source (src.zip), then you can get it manually as mentioned on this SO post: Where to find Java JDK Source Code?
EDIT: Alternatively, you can also use a decompiler (e.g. http://jd.benow.ca/) to reverse engineer the source from byte code, though, it may not be the exact match to the original source but you can understand the overall idea. You can add the decompiler as the default program for opening .class files in eclipse Windows > Preferences > General > Editors > File Associations. Select *.class filter and add your decompiler as the program. Though, it is not as clean as attaching the source to JAR, but may work if you don't have access to source.
EDIT2: About your question
What's the difference between included Jar file and a pure code
Eclipse can find .java files for your own code because obviously they are in your workspace. But when you add a JAR file as library, it may have the source (.java) in it or not. If the source is available, eclipse can display it by default. If not, you have to add it manually.

IntellIj IDEA cannot find source files

I'm trying to attach sources for this library called FileDrop so I can view documentation. When I click Attach Sources... and select the directory with the source code, nothing changes. It still has the Sources not found message at the top of the class. However, in my external libraries section, if I manually expand the library jar file, I can view the original source in there. I think this is because the library jar has the full, uncompressed source in it.
The zip file of the library has this structure after downloading and extracting it:
filedrop-1.1
Example.java
filedrop.jar
FileDrop.java
So the source files do not have any package...not sure if that is part of the problem. The two .java files are also in the jar file. So when I create a library in IDEA, the jar shows up in External Libraries under the library I created, but the source does not.
In the Project Structure -> Libraries screen, it shows the jar file under Classes, and it shows the parent directory (filedrop-1.1) under both Classes and Sources, but it is apparently not detecting any sources within them. This is the screen where I've been trying to add the sources, and they show up here as expected, but apparently no sources are found.
This is IntelliJ IDEA 13.1.4.
The packaging of this library is extremely weird. The jar file contains two different FileDrop classes, one in the default package and another in the net.iharder.dnd package, which seems to be a newer version. It also contains two source files (Example.java and FileDrop.java), the second of which seems to be the source code of the old version of the class (the one in the default package).
The CVS repository at http://iharder.cvs.sourceforge.net/viewvc/iharder/filedrop/net/iharder/dnd/ seems to contain the new version of the code, so you can do a CVS checkout and attach the iharder/filedrop directory from the checkout as sources to the library.

How to mark file as source in Eclipse

I notice that any class not found in eclipse after ctrl+shift+t push.
I notice that I cannot see files, that expose so:
And following classes find good:
How to mark file as source code file in Eclipse?
You need to make the directory as Source Folder in order to make Eclipse treat files as source
You can find the setting in project properties.
Create Source Folder in your Project and copy your source code in there. Follow following steps:
Right click on your project.
select New
create source folder.
Or could it be, that you need to tick the Label Decorations in Preferences->Appearance->Label Decorations "Java Type Indicator"?

Categories