Importing source code of a library in Eclipse - java

I have a question about importing .java files.
I have downloaded source codes of a library that has many subfolders.
The way that I imported is to create a new folder within my project called libs and dragging dropping the entire folder, after that I right clicked into the libs folder and then clicked Build Path-> Use as Source Folder.
Now the issue is most package names are like "analysis.common.src.java.org.apache.lucene.analysis.ar" whereas within their source code their package name is referred as package org.apache.lucene.analysis.ar;
How can I fix this problem? Using CTRL + SHIFT + O does not fix it.

You can use Eclipse File System Import to import sources to a particular project. Here is a step by step explanation.
Another example. Scroll down to "Importing Files Into a Project" section

Related

Importing .java and .class file gone wrong in eclipse

Recently I worked on a project on a mac, using eclipse. When I was done with the project I copied certain .java files and their corresponding .class files onto a USB drive and brought it to my personal computer. When I try to use the import feature on eclipse and import everything, my main method is not recognized by eclipse. It will work if I create a new eclipse "class" by the name I have in the program and copy paste the code. I have many classes so I can not do this for each one. Is there any way for me to change all .java/.class files into files that will be read by the system?
Thanks
An eclipse project contains a lot of meta information. Just open the project directory in a file browser, and have a closer look. There are files like .classpath for example.
When you just copy your source code, you leave all the meta information behind!
You should either copy the complete project directory, or simply use the export task to create a ZIP file of your project. (to later import that in other systems).
There is one other way you can do this. Just create a new project with the SAME EXACT PROJECT NAME that you did at school or wherever. After you create your project, go to your file explorer and navigate to the following place:
<"Directory to work-space">/<"Project Name">/src/
For ex: in Windows, it is Generally:
C:/Users/your_name/workspace/projectName/src
In here, copy all your ".java" files.
Now go to eclipse, right click on your project and hit refresh. You will find all your .java files there. Right-click on your project and hit "Run"->"Run as a Java Project". It should run! hope this helps.
PS: You do not need your class if you are executing it in Eclipse. Eclipse will create its own class files.

How to import OEM Java lib and source into Netbeans?

I have done a reasonable amount of programming in various languages but I am a Java / Netbeans newbie. I have successfully created and run "Hello World" so my Netbeans installation is OK.
I want to write code to control an LED display controller card. I have sample demo code zipped up by the OEM. I am trying to import, run and edit that code. If I can get this running, this will be the starting point of my own code.
The zip files only include library and source files in a directory structure that does not match Netbeans. All of my attempts to import the source and lib files have given fatal errors in Netbeans.
The directory tree has a lib and src branch at the root. The src branch cascades 4 levels to get to the source files.
I figure if I use a different tree structure, I will need to change the import statements in the source file. I'd rather not touch the code until I have it running. I have tried to unzip the project tree directly into the Netbeans directory but that didn't work.
How to import a JAR (Java Archive, the "library" in your case) in Netbeans:
Right click on your project name, in the 'Projects' pane usually located on the left
Click the 'Properties' link from the pop-up menu
Click the 'Libraries' link in the Categories list
Choose 'Add JAR/Folder', browse to your JAR and click OK
If you want Netbeans to create the JAR for the sources you have, do like Stefano says.
You should package the src folder as src.zip with a zip utility and the structure under lib with the java archiver as a jar file.
Now you can define a library in NetBeans and add these files to it (Tools => Libraries => New Library).
For Netbeans to be able to open a project, the folder has to contain some files that Netbeans itself generates when a project is created.
Since you only have src and lib folders available, I think the easiest solution would be to create a new project from Netbeans, so that the creation of those files is automatically managed, and then copying the content of the folders you have in the folders Netbeans will create.
You will also have to tell Netbeans to use those library, and you can do that from you new project's preferences.
Edit
A few more things you can try:
in NB, right click the folder containing the JARs you imported and select "remove", then import them back but selecting the jar files instead of the whole folder;
if you're not already doing so, use the "Clean and build project" button instead of "Build project";
If none of the above solve the issue, try starting from scratch again following these steps:
unzip your OEM files somewhere in your disk;
create a new empty project in NB
copy the files from the src OEM subfolder to src NB project subfolder
in NB, right click Libraries, then add JAR/Folder and select the jar files from the lib OEM subfolder
If this doesn't work either, tell me where I can find the OEM stuff and I'll try it myself :)

How do I use classes from an imported library in netbeans (java)

This must be the simplest thing. I have been trying to do this for hours. I know how to import libraries into netbeans but how do I use classes in these libraries using imports? I have been trying unsuccessfully for hours to use the javacv library on 64bit linux and when I try to import the necessary files from the library netbeans tells me the package does not exist. I want to access a java file example.java inside com.googlecode.javacv.cpp.linux-x86_64 which is inside opencv-2.4.8-linux-x86_64.jar. How do I actually import this so I can do the following: example obj = new example?
Also, in almost every reference to javacv I see this import com.googlecode.javacv.CanvasFrame; but nowhere have I actually seen this in any of the files readily available by google. What's the deal?
Please give this a try, I will be as specific as possible:
Copy the library folder you want to use
Paste it in the "src" folder of your project folder
Once you completed the above two tasks, you can import different classes from the library using the following code:
import nameOfLibraryFolder.*;
The above code will import all the classes from within the library. Replace "nameOfLibraryFolder" with the name of the library folder, to be more specific, the folder that contains all the classes of the library, to be even more specific, the folder that you placed in your project's subfolder called "src".
In order to import a specific class from the library, you need to replace the * symbol with the class name.
For example:
import nameOfLibraryFolder.className;
but if the class is in a subfolder, then:
import nameOfLibraryFolder.subFolderName.className;
I hope this is specific enough, and I hope it helps.
In netbeans, it is rather easy to iomport a external jar and utilize it.
First you need to add jar file in project tab by right clicking on the library node.
Then, in this project, just import the.name in your java file.
This should work, I have used Netbeans for three years
You many need to do one of the following option.
Add an new Library to NetBeans, then add it to you project
If you Tools -> Library a popup will come up.
There you can add a new library. Click on New Library and name the new Library.
Import a library from the ClassPath tab Add Jar/Folder
If you want to can also add the javadoc from the JavaDoc tab. You would add the file from the library file you downloaded, called javadoc. This will allow for the javadocs to be seen while editing your code.
Also you can add the sources file form the Sources tab. Import the file named sources
Then click on the Libraries folder icon in your project and Add Library
Another option is just add the .jar.
Right-click on the Libraries folder icon in your project and click Add Jar
Locate the .jar file and add it.
As long as you have:
package MyProject;
At the top of your project Java wont recognize it.
* It seems to be like some sort of sandboxing affect.
* To use your classes you have to open the directory you
* Saved the project to and Copy/Paste the library to the source file.
* I also found that simply deleting the named package works as well.
* But now you're no longer linked to that package. -shrugs-

Importing class/java files in Eclipse

I'm new to Java Programming using Eclipse, so would really appreciate your help for this question.
I have doing Java programming both at college and home. At home, I have all my Java classes under a single project-(Java Reference). Now, in college, the java programs are compiled on the lab machines. And I would really like to have all these programs in a single project, on my laptop.
I had recently copied all the .class and .java files from the programs we did in college labs, so I could have them under the "Java Reference" project in my laptop. I copied the .class and .java files under the "bin" and "src" folders of the java project, on my laptop. However, I don't see anyway to import them through Eclipse.
From what I read here, it looks like having an entire project(created somewhere else) could be copied into one place, through the import option, but in my case, I only have the .class and .java files.
First, you don't need the .class files if they are compiled from your .java classes.
To import your files, you need to create an empty Java project. They you either import them one by one (New -> File -> Advanced -> Link file) or directly copy them into their corresponding folder/package and refresh the project.
You can import a bunch of .java files to your existing project without creating a new project. Here are the steps:
Right-click on the Default Package in the Project Manager pane
underneath your project and choose Import
An Import Wizard window will display. Choose File system and select the Next button
You are now prompted to choose a file
Simply browse your folder with .java files in it
Select desired .java files
Click on Finish to finish the import wizard
Check the following webpage for more information:
http://people.cs.uchicago.edu/~kaharris/10200/tutorials/eclipse/Step_04.html
create a new java project in Eclipse and copy .java files to its src directory, if you don't know where those source files should be placed, right click on the root of the project and choose new->class to create a test class and see where its .java file is placed, then put other files with it, in the same directory, you may have to adjust the package in those source files according to the new project directory structure.
if you use external libraries in your code, you have two options: either copy / download jar files or use maven if you use maven you'll have to create the project at maven project in the first place, creating java projects as maven projects are the way to go anyway but that's for another post...
I had the same problem. But What I did is I imported the .java files and then I went to Search->File-> and then changed the package name to whatever package it should belong in this way I fixed a lot of java files which otherwise would require to go to every file and change them manually.
import class folder does not work for me, but add jar worked!
1. put the class folder under the project folder
2. Zip the class folder
3. Highlight project name, click "Project" in the top toolbar, click "Properties", click "Libraries" tab, click "Add External jars".
4. Add the zip file. Done!

Where I could find the Import file in eclipse

I just want to know where does Eclipse store an import file like
import com.codecarpet.fbconnect.FBRequest.
Eclipse doesn't store the imports separately. You can browse the packages using Package Explorer window (mostly found at the left side of window).
Eclipse doesnt store anything for every import. Once you keep adding libraries to your project build path there is a .classpath file that is updated. It contains the list of all the libraries that you project depends on.
The .classpath file looks something like this:
Hover your mouse over FBRequest in the import statement, hold ctrl, a hand appears, click on it.
In package (or project) explorer, the view normally to the left click the two arrow symbol link with editor.
Click on the editor where your class file is, the editor won't look like normal JAVA.
Now you are in the jar file where the class file of the import statement is
Browse up In package (or project) explorer to you find the jar file and right click for properties. There you have a field location and there is were the file is on the file system
Eclipse does not store anything. You tell to Eclipse where to find referenced libraries. Please open project properties (right click on project + properties) and select "Java Build Path" from the list at left, then choose "Libraries" tab
That's plain Java. Eclipse doesn't store anything, you have to put the containing library in your classpath. You can edit your buildpath by right-clicking your project in eclipse --> build path --> edit build path. You can add the needed class-files (or libraries) here.

Categories

Resources