how to import a java class by a jar file - java

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.

Related

Use classes in jar file in java program in eclipse

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.

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!

Using library defined in global library

I have created a new class library and I've defined it in the global libraries with the name Encryption. I've also added the jar file in this newly created class library. My question is how do I use the this jar file in my other projects now? Do I need to import Encryption in the class I am referencing it from?
This is the print screen of how I'm trying to add jar file in dependency folder and the print screen of the project properties folder:-
Once you define it as a global library using netbeans option, you need to do following
Right click on project -> Libraries
Go to Project properties
Compiler Tab
Add Library Button --> In global Library list you should be able to
see that library.
Create a library from your Encryption jar file
Add this Library to project where you want to use
Then add that global library to your local project, in which you can import the Encryption class and use it.
Even if you make a .jar file for your library classes, you'll have to import it. For netbeans import of libraries below is a screenshot -
Edit
Why don't you try Add Dependency?
Instead of adding the jar via "add local resources" after creating the dependency, I tried using the option "manually install artifact". This allowed jar file to become available in my dependencies and I could use the class files by using a simple import statement referencing the path to the class files in that jar file.

How to use your own created jar file in another android application

I created my own jar file using eclipse. Now i am trying to make an android JAR file and then will use the same JAR file in others android apps.
Now though, my application is complete and make a jar file, but how i will use it for other Android Apps?
You should be able to access any publicly exposed methods from your .jar by importing the .jar into your project, then adding
import myjar.myclass;
You can then reference any methods from the class you import.
Hope this helps!
Add the jar to you build path
Right click project
Click "Properties" -> "Java Build Path" -> "Libraries"
Click "Add External Jar"
Import classes like normal
import com.my.package.here.MyClass;

How do you "install" Simple in eclipse?

How do I use simple in eclipse?
Do I add it to the JDK?
http://simple.sourceforge.net
The web site seem to have allot of info on using the methods but little on setting up the library.
I have used C and C++ to set up external libraries before but the Java set up confuses me.
Thank you!
In java world, in order to be able to use third party libraries, what you need to do is to add them to your project/application's CLASSPATH. In case of Simple, its library file is simple-xml-2.4.1.jar that is located inside a folder "jar" within the simple-xml-2.4.1.zip that you would download from Sourceforge.
Download and unzip the
simple-xml-2.4.1 Archive. (ZIP file).
Note that it has a Jar folder that
contains simple-xml-2.4.1.jar - that
is the library eclipse and other IDES
would use to set up the CLASSPATH
Goto your project in project
explorer.
Right-click on the project name and
select "Properties". Alternately, you
can click on the project name and
type "Alt+Enter". A window will
pop-up.
In the window, select "Java Build
Path". You will see several tabs,
including "Source", "Projects" and
"Libraries".
To add simple-xml-2.4.1.jar to your
project, click on the "Libraries"
tab. From there, you can add either
internal jars or external jars. The
"Add External Jar" would be to add a
jar that's on your computer somewhere
. So choose that button, browse to
where simple-xml-2.4.1.jar is located
and make sure to add it to your build
path

Categories

Resources