Need to add the javax.mail package to my eclipse - java

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

Related

how do i put utilities.java into a library as a folder so that I can use its methods with multiple java projects?

I have a utilities file named utilities.java that is located in my ...\NetbeansProjects\Utilities folder and I want to use the methods in it in any java project that I work on.
If I right-click Libraries folder, there is an option to include JAR/folder... so I select that and find and select the Utilities folder and Absolute Path to it as shown, which adds the Utilities folder to the Libraries folder, as shown.
But how do I reference the methods that are in Utilities.java?
import doesn't offer any help and all I get on every attempted reference is Cannot find symbol.
I can't turn Utilities.java into a .jar file (can I??) because the option to Compile is grayed out.
What do I prefix to each such unresolved external reference to successfully link such methods to my code?
Whether I try to just import or import static, I get error, with suggestions to change to some inappropriate import.
What's irritating is that I think I've successfully done this before....
Based on your screenshot, it looks like you are using an older version of NetBeans (perhaps v8.2 or similar). I will assume this - but it would be worth upgrading if possible.
Generally, I would recommend creating two NetBeans projects, and then include the utilities project as a dependency in the application project.
You can use the "Library" features of NetBeans (as shown in your screenshot) but I think it is cleaner to just create projects, which can then be managed by Git/Mercurial/etc. as needed, as separate projects, with no need to rely on NetBeans-specific library management features.
Ant-Based Approach
For an Ant-based approach (the default for NetBeans 8), that would be something like this:
The above was created as follows:
Create the utilities project:
File > New Project > Java > Java Class Library
Project Name: MyUtilities
Create your package and your Utilities.java file. Add the code you need.
Clean and build the project.
Create a project to use the utility JAR:
File > New Project > Java > Java Application
Project Name: MyApplication
Right-click on MyApplication in the project explorer and select the Properties pop-up window.
Libraries > Add Project - find the MyUtilities project and select it, click on "Add Project JAR Files".
Now you will see that project's jar file listed as one of MyApplication's library dependencies.
After cleaning and building the project you will see a file structure as follows:
Note: For NetBeans 12, it's similar, except they now make the choice of an Ant project (vs. Maven, Gradle) explicit, rather than the default.
Maven-Based Approach
It's a similar approach: Two separate projects. In this case they are both "Maven > Java Application" projects - there is no distinction, in contrast to the Ant approach.
The main difference is how you include the utilities JAR in your applications. that depends on how you want to manage your Maven artifacts - and is a bit outside the scope of this question. One way is to follow the guidelines here - but I think there are other approaches, also.

Jar file too big

I created a simple project using WindowBuilder in Eclipse and my goal is to send it by email. I don't know what kind of computer the person is using, so I exported my project into a 'Runnable JAR File' and checked the option 'Extract required libraries into generated JAR'.
The problem is that the generated JAR file is 20MB in size!!! The project has only one simple window - nothing complicated or fancy.
I found that some people use ProGuard to include only what is really needed.
I would like to know if there is a way to optimize it 'manually'? Are there libraries that are automatically included when creating a WindowBuilder project, and how may I determine which libraries I can remove?
Thank you.
I've had the same Problem using WindowBuilder. My solution was to look for the imports in my .java file, e.g.: import org.eclipse.swt.SWT;
In the Project Explorer in Eclipse you can see that there are much more imports than needed. Those 'Build Paths' can be removed carefully.
Simply rightclick on a .jar import like "com.ibm.icu_52.1.0.v201404241930.jar" and click on "Build Path" and "Remove from Build Path". Unfortunately, you can't remove or delete the packages inside the .jars.
But using this method, I managed to reduce the size from 20.7MB to 3.87MB. It's something...
Like fge said, send the source. Or upload the .jar to dropbox if you require you partner to access all of the files in the .jar.
If you don't like dropbox, find some other cloud storage solutions through a google search. Github or Bitbucket could also be an alternative to dropbox.
Cheers.
Use the Eclipse plugin FatJar. FatJar shows you which libraries are getting imported and allows you to specify which .jars, libraries, etc., that you want built into your JAR.
FatJar
Your jarfile is too big due of the project template. Create a sample java project, then use windowdbuilder components. Do not create a project Swing Template, by this way eclipse is gonna import all eclipse gui toolkits.
Tried it out with my way, my application's length equals to 3Ko against 24MO with the template project

Eclipse - External jars need to be manually added

I had a question that was answered with adding jfxrt.jar to standard Eclipse build path.
I followed the suggestion, adding the jfxft.jar as an External Jar on the jdk1.7.0_10.jdk JRE i have installed (under Eclipse > Preferences > Java > Installed JREs).
But when I import "javafx.application.Application"
It errors with:
Access Restriction: The type Application is not accessible due to restriction on required library /Library/Java/JavaVirtualMachines/jdk1.7.0_10.jdk/Contents/Home/jre/lib/jfxrt.jar
There are posts on this such as Access restriction: Is not accessible due to restriction on required library ..\jre\lib\rt.jar
I can fix this possibly by the suggestions in that thread (though I'm not convinced they are good solutions).
Or I found a better solution of adding the External Jar directly to the project:
- go to the JRE and remove the External Jar as setup above
- Open project properties and go to Java Build Path
- Go to Libraries tab
- Add External JARS... and choose exactly the same jfxrt.jar
No access restrictions now!
Why does it work one way but not the other? The advantage of adding directly to the JRE configuration is that it only needs to be done once.
Thanks,
Hank
I would not recommend adding JARs to the JRE configuration like that, it's just too easy to forget they are there and that can lead to confusing behavior if you don't expect that particular JAR to be on the build path for a particular project. Also, what happens if one project wants to use a different version of the library?
There are at least 2 other options that I would consider:
Define a User Library for JavaFX and then include the User Library on the projects that need it. You still would have to add the User Library to each project that needs it, but that's not such a big deal IMO, as it only has to be done once for each project.
Create a separate project to contain the JAR(s), call it something like "JavaFX Libs." Add the JARs to its build path and make sure to export them on the Order and Export tab; then add "JavaFx Libs" project as a dependency for whatever projects need it.

The question on configuring build path which involves a lot of libraries

For a newly created project B, I need to build the path the exactly same way as an existing project A. After opening the “java build path” window by clicking “build path--> configure path”, I found that There are a lot of libraries involved in Project A. How to handle this kind of scenario? Add those jar files one-by-one?
If you can locate all those jars on your file system/network, the best thing for you to do is to create an eclipse User Library.
Creating a Library
Window >> Preferences >> Java >> Build Path >> User Libraries
Select New
Give your library a name
Ok
Select Add Jars
Add all the jars you need for your library
With this library created, you can share the same jars between projects easily by importing the library as opposed to each individual jar file.
Importing a Library
For each project that needs a particular library all you have to do is import the library.
Open the project's properties
Select Java Build Path
Select Libraries tab
Select Add library
Select user Library
Select your Library you created
Finish
Note
User Libraries do not belong to projects, they belong to the User but can be referenced by multiple projects.
Using libraries allows you to update your jars only once for all projects that share the same jars.
I hope that helps :D
If you are using Eclipse and you want your new project to have the exact same libraries/dependencies as a previous project, you can copy the appropriate contents of the .classpath file from the old project to the new one.
This would include any classpath elements with a kind="lib" attribute. You may have to update the path attribute if the location is relative.
This assumes you aren't using something like Maven for dependency management...
There are 2 options:
Create a User Library.
Window > Preferences > Java > Build Path > User Libraries
The good thing about this is, you can re-use it provided you need exact set of libraries for other projects.
Bad thing is, if you want to add new JARs to this, you have to modify it. And modifications is .....not difficult, but takes time. Also, if you need slightly different set of JArs for different projects, then either you have to add some not-needed JARs in either/both projects or you have to add this User Library + some external JARs.
EDIT 1
Go into the Eclipse preferences > Java > User Libraries. Click on "New.."
Choose a name for your user library. Leave the "System Library" checkbox unchecked. Push "OK".
Select your library and click "Add JARs...". Navigate to the location where you put the library.
For each JAR file, there are 4 additional parameters you can attach to the JAR: Source location, Javadoc location, Native library location, and access rules. Except the rest 3, Native library location is not an optional field, and must be included if the JAR requires one
OR
Simply add as External JARs.
Good thing: Simple, straightforward.
Bad thing: If you need too many jars, you have to keep track of each separately.

sHow do I add external JAR files in a Web Free Form Application in NetBeans?

I have a Red5 application that i want to work on using NetBeans 6.8.
I know I can use a web Free-Form Application, the only problem that I'm having is to add external jar files.
When I go to the project's properties, Where exactly do I add the external jars that I need in order to properly compile the application?
update
i think that the following URL addresses my problem but i can't really understand the solution.
http://www.bradmcevoy.com/blogs/netbeans_ant_ivy.html
You can add the external libraries to your project's Libraries folder, or you can add them in the NetBeans' Tools > Libraries dialog. The latter can be shared among several projects. This answer includes illustrations.
Addendum: For free-form type projects, the Projects > Properties mentions, "Any settings modified on this panel are for IDE purpose only and do not change the build script. If you want to make changes in build process, please modify your build script manually."
found a solution here but it's probably for a lower version of netbeans:
http://forums.netbeans.org/viewtopic.php?p=5329
i get an error on the tag.
Since it is a free-form project, you have to make sure that your build script puts the correct jars into the compile-time classpath. You also want to make your Netbeans project libraries match for auto-complete purposes and error highlighting and such (ie when you view Project Properties->Java Sources ClassPath).
Ok I finally resolved the issue by re-creating the project using the step by step tutorial in the following URL : http://blogs.oracle.com/coreqa/entry/setting_up_freeform_project_correctly

Categories

Resources