I have converted my java code into .jar now i want to use the class in that jar file in another class, so i am unable to do so. Thank you very much for any kind of help!
In Eclipse, right click on your project where you want to use your jar. Select "Properties", there select "Java Build Path". Under "Libraries" you can add the JAR (if you copied it to a lib folder in your project), or select an external JAR (if you don't have it in your lib folder).
When this is done, you can use it in your code. Just use the class, and when a red scribble is underlining your class name, hover over it and select "Import 'Classname' (this.is.your.package)". Alternately, you can let Eclipse auto import the most logical package by pressing "CTRL+Shift+O" (hotkey to auto import)
Add the jar in the new project classpath and then you should be able to use the classes.
Related
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!
I had a problem when importing org.slf4j.Logger and org.slf4j.LoggerFactory, how to download these two classes and if it is by the mean of a jar file, how to import from it.
To be able import any classes into your project, such classes should be added to the classpath of your application.
Usually you won't need to extract classes from library, so in your case you need to add slf4j-api-1.7.5.jar from slf4j-1.7.5.zip which accessible here: http://www.slf4j.org/download.html
Here you can find example how to add library for eclipse project: http://www.wikihow.com/Add-JARs-to-Project-Build-Paths-in-Eclipse-%28Java%29
If you do have the corresponding Jar file, you just have to add it to your project. If you are using Eclipse IDE, you can click on "Project" menu, "Properties", "Java Build Path", "Add External Jars..." and then a file chooser will appear. Choose the location of your Jar file and open it. Here you go. If you are using another IDE, just specify it, so we can further help you with respect to your IDE.
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.
Is it possible in Eclipse to create a separate Folder in a package? When select "new --> folder", my eclipse version always creates a package. But i want the new folder to be in the old package.
How can this be achieved?
Thanks in advance...
Patrick
What you might want to do, if you aren't putting code in that folder, is remove the folder from the build path. Right-click on the offending folder in eclipse and choose 'exclude from build path.'
Or, you may want a different effect:
Right click on your project and choose add new source folder.
Then in that folder, add the correct package folder structure.
You can have files in the same package located in different folders.
You need to exclude the folder from the source path
In a source folder, all 'new' folders will be considered a new package. It's part of the Java spec that makes all folders from a root to be part of the package structure.
As others have said, this is standard java behaviour and shouldn't really be worked around.
You are probably better off creating another folder outside of the source folder.
However there is a way to do it in Eclipse, be warned that this might come back to haunt you if you later try to build outside Eclipse as other tools won't honour these settings.
You can exclude a folder from the source path by opening the project properties.
Selecting Java Build Path->Source
Select the appropriate source folder and select the Excluded: child item
Hit the Edit... button.
Select the Add button next to the Exclusion patterns: pane
Add your folder and Finish/OK back to the editor.
The matching resources will now be excluded from compilation within Eclipse. It will still appear as a package in the editor though.
You want to create a new child package (bar) of the existing package. (com.foo)
Select new package and name it "com.foo.bar". Eclipse will correctly place it under com.foo.
FYI: Folders under any "source" folder are shown as packages. There is no physical difference except that they appear on the build path. As shown elsewhere you can exclude them but it's easier to just not put them under /src/... to begin with.
Hey all. I am working on a project for school where we are given the .class file but not the source to include in our code. I am using Eclipse, and I want to include the file in my project so I can instantiate objects from it and use it. The file is TokenizerImpl.class, and I want to use it like this: TokenizerImpl tokenizer = new TokenizerImpl(foo);
I put the file in my project folder, and Eclipse says that "TokenizeImpl cannot be resolved as a type", which I assume means it cannot find the class or source. I tried putting it in the "bin" folder of the project and got the same error. Google search and SO search didn't seem to answer this, so I will give it a shot. How do I do this, oh wise ones?
Edit: Oh dear, I found the problem was something else entirely. These solutions worked fine, but I just forgot to create the Tokenizer interface that TokenizerImpl implements. Doh. Thanks for all your help though, I did learn a lot about eclipse.
You can add a directory containing the class files to the Eclipse project, only if it is inside one of your Eclipse projects, either in a generated directory or in one you have created.
This can be done by adding the class folder to the Java build path of the application. You can set this in the Project properties, by visiting Java Build Path -> Libraries -> Add Class Folder. Keep in mind, that you will have to specify the root folder containing the class files in their packages.
Therefore, if you wish to have the compiler access com.stackoverflow.Example.class present in the classes directory under project A (but not in the build path of project A), then you should add 'classes' as a class folder, and not classes/com/stackoverflow as a class folder.
Project -> Properties -> Java Build Path -> Libraries -> Add External Class Folder
The folder must contain a package hierarchy, i.e. if your class is really foo.bar.TokenizerImpl it must be in the subdirectory foo/bar.
You could also JAR the class files that you want to add and add the JAR file to the build dependencies. To me this is the cleanest solution. Internally the JAR file has to have the correct directory structure, of course.
Right click into your project and select Java Build Path to add new dependencies.
Copying it into the bin folder won't work very well because it is meant to hold the result of compiled sources. As soon as you clean anything the file will be gone.
or put everything into a jar file and add this as an external jar.
Other people have now given better answers. This "answer" was mainly to get information from the OP because the original question didn't really tell us, fully, what had been tried. There are now two answers that truly answer the question in a long-term way.
My original answer is left below for context.
Did you copy it to the bin folder within Eclipse or outside Eclipse? If you did this outside Eclipse then you have to right click on the "bin" folder and select "refresh" for Eclipse to see the new file.
A *.class file in the appropriate folder (depending on its package) under the bin directory should do it.
zip the class folder.
Highlight project name, click "Project" in the top toolbar, click "Properties", click "Libraries" tab, click "Add External jars", add the zipped file