Importing jar file into IntelliJ Idea? - java

I want to use methods in Java Mail API, so I am trying to import its jar into my project on IntelliJ IDEA. How do I import or download this new jar? I am not new to IntelliJ IDEA, but haven't imported new jar in two months now.
Also, do I need other setup in order to use Java Mail API?

1) You need to create a Project in IntelliJ IDEA.
Now you have two options.
2.1.) Open the Project view in the IDE. Then right
click on your Project and do Open Module Settings (or press F4).
2.2.) From the File menu just choose Project structure.
3) Then go to Libraries on the left side. Then click the + sign (add new library).
Then locate your JAR and select it, this will import it as a library.
Note: The JAR you need to have downloaded first on your local machine.
Here is a screenshot, with added one JAR to a project I just created.
To make an example that works you'll need some mail servers.
See for example here.
http://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example/

You can go to 'Project structure' with this short key 'Ctrl+Alt+Shift+s'
and in Library you can add your jar files

Related

Need to add the javax.mail package to my eclipse

I want to create a basic java application where I send and receive an email, but my Eclipse doesn't recognize the javax.mail package which I need for the code.
I've downloaded the package, but I can't figure out how to add it to my Eclipse.
I also would prefer that it gets added to my Eclipse workspace and not just to the one project that I am doing, as I will need the package later for other projects.
How do I install the package? I don't know how to properly use things like class paths and command prompts etc, so I need a step by step help if it requires that.
If that's really what you want to do, go to Preferences -> Java -> Build Path -> User Libraries.
Then press new... give it a name and add the jars, which should be ideally located in an appropriate place that makes sense to you and especially not in the JRE directory.
Then you can use those libraries in any new project. For that go to properties of the project, Java Build Path and add the libraries in the (surprise) Libraries tab.
But I think you would be better off using Maven, Gradle or any other build tool in your projects. However if a library is not publicly available in a repository, and you must download the jars, then this a reasonable option.
Here are some images illustrating the process:
1. How to add new libraries to Eclipse
2. Edit library settings (adding source, javadoc and so on)
3. Add your user library to a project

Adding External jar copy to Project

I created a project but I am using an external library for it. enter link description here
I added the library to my Beans as follows:
Right click on folder "Libraries"
Add Library
Create...
Gave it a name "gson 2.7"
Picked the class, source and javadoc files.
I went on and continued programming and then I added my code to Github. Then when I cloned it my Library was missing.
How can I fix this ? How can I make it dynamic so I dont have to worry about this anymore. In other words I need to add the library to my code and not just the reference.
Thanks a lot for the help
Just upload a copy of your library to github.
I have not used netbeans, but from what I understand it adds the jar somewhere outside of your project directory, and it simply adds the location of the jar to your classpath. When you upload and then clone the project from github, the jars are not uploaded/cloned, and the project structure metadata for your project is also lost from netbeans.
My suggestion is to learn and start using a build tool like maven. It takes up the responsibility of adding your dependency libraries everytime, given that you have an internet connection. It also adds the libs to the target directory of your project folder, so you probably will need the internet connection only once. The libs will be uploaded and cloned back from the github repo everytime.
IDEs like eclipse have inbuilt support for maven, so you don't even have to worry about executing the mvn commands. Here's a tutorial that will get you intrigued - with the IDE that you prefer.
https://platform.netbeans.org/tutorials/nbm-maven-quickstart.html
Thanks a lot for all the replies and comments.
Technically there are two approaches to my problem. For this articular example I chose to copy the external jar files to my project.
1. Copy jar files to your project.
Go to project properties
Under Libraries folder make sure to set up a libraries folder by following the steps.
Add Library at the bottom ( you can create or import; since I already had gson in Netbeans I just imported it.)
Press ok to confirm and you are done!
2. Use Maven
Go to plugings and make sure you have all Maven plugins installed, if not do so.
Create an new project and choose maven/java application
Follow the prompts
At this point you can start coding (in my case I just imported my class files from the other project)
Right click in "Dependencies folder and then "Add Dependency"
Under query type the repository you are looking for; in my case "com.google.code.gson : gson : 2.7"
Click add and you are done!
Thanks a lot to everyone for the help :)

Android Studio 2.0 using external projects with jar and source files

i've been searching all the web for a definitive solution for this simple problem of adding an external directory with .java files and .jar libraries to my Android Studio project.
Here's what I want to do:
Create a default project in Android Studio, and import from git the library I will work on, which is a bunch of .java files and a .jar (jsoup). These java files need this jsoup. The java files are not for Android, just simple java files.
I need it to be from github because I'm working on this lib and I need AndroidStudio to automatically update it to its newest version.
This is how I compile my project:
javac -cp ".:jsoup-1.8.3.jar" Main.java Class1.java Class2.java;
Then I run:
java -cp ".:jsoup-1.8.3.jar" Main
Well, I already loaded this library from github using Android Studio, so now I have:
Project/app/src/main/java/com.myname.myapp
Project/MyLibraryFromGithub
Of course I seen answers like this and I followed everything, but there are some problems. For example, it kept searching for a AndroidManifest.xml file which I don't know how to write because it's a simple java project, not a Android Project. Also, I think this answer is outdated, and won't work properly in AndroidStudio 2.0.
I've read some tutorials on Gradle but it won't say anything about external folders and so. Also, when I make it happen, how do I import my library? For example, in
Project/app/src/main/java/com.myname.myapp/Main.java
How do I import the library? import myLibrary? Or I just need to create new objects with the classe's names? Like Class1 myObject = new...?
This is so congusing! I whish I could compile everything by hand, Gradle is hiding too much from me and I can't find a good tutorial.
To add an external jar to your project you should do the following steps:
Select File >> New >> New Module.
Select Import JAR/AAR Package.
Give the file name in the File name text box and click on Finish.
Select File >> Project Structure.
Select app under the modules section on the left hand side.
Choose Dependencies tab at the top.
Click on the + on the right hand side.
Select Module Dependency
Select the JAR file that you have just imported.
Close both the open windows by clicking OK
Have you tried this?
Place X.jar into the libs folder
Right click on X.jar and 'Add as Library'
(In build.gradle) Place this in dependencies: compile files('libs/X.jar')

Referencing .jar files in NetBeans

I am trying to use FEST automation software. I am currently dealing with NetBeans 6.8 because it is what I have to use at work and am unable to upgrade the software at this time because of our system requirements
[TLDR: don't tell me to upgrade NetBeans because I can't]
I have downloaded the jar files from the google code repository and am trying to get a very simple application to work. I have tried adding the jar files to Libraries through the property panel for my project. NetBeans still cannot find these methods. Where else/how do I add these jar files to the class paths?
In order to use .jar files in NetBeans, you can either add the jar file to your global libraries or your local libraries. It sounds like you tried doing it locally without much success. I recommend adding a library such that you can just pick from your list of libraries without needing to reload it. I use NetBeans 8.1 so it may be just a bit different for 6.8.
What you need to do is go to Tools->Library
Then you need to click on New Library at the bottom left of the dialogue box.
AddLibrary
Name it and hit 'Ok'. Then, you'll add a new jar file by specifying your jar path. Hit 'Ok' again to save everything.
In order to use your new library, you'll need to then click on your Libraries folder in your project and add a new library. Select from the list of libraries the library you just created.
SelectLibrary
Finally, you import your individual files in your library using the #import keyword as per usual. If you want to import everything, you'd type something akin to
import MyNewLibrary.*;
To load a library the way you tried to do it, you'd right click the libraries folder and click "Add JAR/folder" instead of "Add Library". Then you'd import everything.
If you still for some reason cannot get your library to work, then you probably aren't importing using the right name. Or, you might be trying to load a static library without using the static keyword.

loading a java project

I am currently studying at university and based on other peoples recommendations I am beginnning to read other peoples code.
i have found and downloaded the source for an open source java project, that seems quite interesting. I want to load this project into an IDE (netbeans or eclipse), so as to make it easier to read and navigate. However both IDE's say that the project is not recognised. It is not only this project, but any project I have attempted to load.
How do I load a project (ie. a file structure of source files) into the above IDE's as a project.
Thanks in advance,
Sam
In Eclipse, select File, New, Java Project, give the project a name and then select Create Project from Existing Source. You should then select the src folder that contains the code that you'd like to use.
Eclipse should then create a new project containing the source. To change the project settings e.g. source folders/external jars, right click on the project in the Package Explorer and select Properties, Java Build Path, Source and make any appropriate changes.
Actually there is a cool trick with Eclipse, you should be able to just select the class that you are interested (select all of the text from your browser), and just paste this in Eclipse right on top of a project, it will automatically create a class for you. It's very handy when working with SWT snippets for example.
You need to have created an Eclipse project first. But you can just create one project and put any number of classes in it.
If the open source project in question uses Maven as the build system (i.e., there is a pom.xml file in the root of the project) and you have Maven installed, you can run 'mvn eclipse:eclipse' to build the project files for eclipse to use. Then just use File > Import and select existing project into Workspace and point it at the root of the project folder. Eclipse should then import the entire project with the appropriate libraries. There are also plugins that work for NetBeans and Idea I believe.

Categories

Resources