Sharing user libraries between projects - java

I'm taking some time to learn Eclipse, and later on NetBeans. I actually enjoy both IDEs.
I don't know which one I prefer, but they seem to be intuitive.
As for the Eclipse IDE, I'm learning about best practices for sharing my User Libraries amongst all my projects. Not to mention when I find someone else's Java code online, I certainly would like to import that code and utilize it in any place I need.
I notice there's many questions on Stack Overflow about how to share assets among all the Projects.
To be clear, when I say "assets", I mean anything other than where I would have my Project's main method reside. (ie: images, xml/txt files, custom classes, etc....)
I come from the web development world (PHP), so my mindset is accustomed to PHP's include() and require() functions, for importing anything on the server into my application. Like most web developers I know, I keep my web assets in the same places for ease of importing.
Now onto a new world, Java + Eclipse.
I've created a single Project, and I am now creating additional Classes within that single Project. These Classes could be useful in practically every Project I create.
After reading other questions and posts I'm still not quite sure of a Best Practice for asset sharing between Projects. (I'm only concerned about a single workspace too)
I'd like to hear from others about their personal Best Practices.
Do you create a Project that holds only Classes, Images, Files?
Do you keep a Folder/Directory somewhere on your File System that keeps all of that, and you import it?
Do you utilize Eclipse's default settings when creating a Project, or do you change anything around?

You can put the shared libraries in an Eclipse projects P and make other projects depend on P. To make a project Q depend on P do the following:
Select project Q in the package explorer, and right-click on it.
Select Properties from the context menu.
Go to the Java Build Path properties page.
Go to the Projects tab.
Check project P to add it to the build path of project Q.
See the article at http://www.informit.com/articles/printerfriendly.aspx?p=367962 for more details.

There is another better way of doing it.
Using Projects as dependencies for other Projects may seems easier... But that should be only done if we want to use that project as library project.
Else if we just want to do sharing user libraries between projects, than following approach is better.
Create Linked Folder containing libraries, as follows:
Right click on project.
Click on New, and select new folder.
Go to advance.
Choose your lib directory.
Click on ok.
Right click on project.
Go to properties.
Go to Java Build Path.
Go to libraries tab.
Click on Add Jars... and import required jars from /libs directory.
Click on ok.

Related

integrating another package/module in my app

I would like to use the following module (https://github.com/intrbiz/hs110/blob/master/src/main/java/com/intrbiz/iot/hs110/HS110Client.java) in my app to control a TP-Link smart plug but cannot figure out how to get it done. I managed to add the dependencies for fasterxml/jackson dependencies but searching the dependencies for "com.intrbiz.iot" did not return any matches so I guess I need to copy and integrate those files locally but I am not sure how to do that (each function in the HS110Client.java file is defined in a separate .Java file (e.g. HS110Response.java, GetRealtime.java...etc.).
Any help would be much appreciated :)
Thanks!
Gaetano.
Nice work on jarring up the project. I cannot go into all the specific details on this post but I can try to guide you in the right direction from here. Try to view the .jar as an artifact that you will use in your codebase. 2 options from here are listed below, google each one as you will find many articles to help you figure it out.
Add the .jar file to your build path manually (usually something like right clicking your project in an IDE -> configure build path -> add dependencies)
If your using Maven (very common in todays development world) you can convert the .jar into an artifact and include it in your pom.xml
In my case I use maven, once you get the dependency in there your IDE might look something like below, note the "maven dependencies" section, it basically lists all the .jars in my project that I can consume.
Once you get the .jar in your project correctly the source code should become usable. Most IDE's use a hotkey as ctrl+shift+t (open type) that allows you to search for classes, if you can do that and see the classes from the .jar your built, you should be able to simply import them into any class your working on and use it directly.
Note: If you are using maven another thing you should check is if this project your consuming is already in the maven central repository, if it is, you can just add it to your pom.xml and your good to go.
Good luck and hope this helps!

Eclipse (java) web services, what files to version and how to properly pull the project to a new computer?

Alright, so I have a web service that was created using an eclipse dynamic web project. It is currently shared on a CVS repository, but the versioning system used is irrelevant. At the moment, I have literally NEVER been able to pull this project out as is and get it working. It leads to countless errors that cannot be fixed. Every time I need to work on this webservice in a new machine I have to create an entirely new dynamic project, copy over the source files, add all the necessary libraries and make the deployment assembly work correctly again. After finally making it run I share the project as the same one, stop after a second, and then synchronize again (in a way tricking eclipse into thinking this was the shared project all along).
I feel like others must have run into this problem and found a way around it. So if you have a web service or any dynamic web project, what files do you share, and how do you successfully pull it from the repository and get it to run on another machine besides what I currently do now?
Your help is much appreciated,
-Asaf
Edit: After reading some of the responses I feel that this question is actually more specific to those who use WTP to create/test their web services. Just wanted to add the clarification.
Edit2: Let me also clarify that the other 20 or so projects not using WTP are shared just fine. I am able to pull and run them with no problem. Only web service projects are an issue.
In general, you want to check in everything that's not "derived" (generated or compiled - that's usually the contents of the bin directory or other place where your code is compiled/built into). For Eclipse Java projects, you want to include the .project, .classpath, .settings, and any other similar files that Web Tools might create for Dynamic Web projects. The Eclipse CVS client will ignore files marked as Derived so you shouldn't have to worry to much about it.
Without more detail about what kind of problems you've run into, it's not possible to guess what was causing them. My only guess is that perhaps you had different versions of Eclipse and/or the WTP (Web Tools Platform) plugins installed on the different machine. That's just a wild guess, but could explain some incompatibility when you check out the project from CVS.
Bottom line, checking in those .* files is the long recommended approach from Eclipse gurus. Maven can kind of change things, but you didn't mention it so I'm assuming you aren't using it.
I am primarily sharing my experience, may be you can find some help.
Conceptually speaking, the files which the IDE can generate itself while creating new project should not be pushed. I.e the IDE specific files should not be pushed. And everything which the IDE cannot generate on its own must be pushed.
Forexample in case of eclipse, following files should not be pushed:
.settings
build
.classpath
.project
For setting the project on new machine, first pull the files from server, and then create a project from IDE using pulled files.
EDIT: If your project has external jars/libraries, then you will have to add to the classpath manually. You could also push .classpath but that might give errors while creating a new project.
I think it's easiest to use a build system and let the IDE generate the project from your build system.
Eclipse, Netbeans, and Intellij are all pretty good at building projects from maven or ant build files. With this solution you have a simple build that is easy to setup in CI (Hudson, Bamboo, whatever) and you don't have any IDE specific files checked in. If my workspace is totally different than yours, with different versions, plugins, whatever, I'm not stuck with your project file and you're not stuck with mine. My IDE creates the project appropriate for my environment and your IDE does the same for yours.
Since you mentioned having to manually add libraries, I assume you are not using any build manager (like, maven or ant) besides ecplise.
For ecplise to handle the project properly you need the source files (*.java) in their respective directories, any resources bundled with the web service (e.g. services.xml), the ".project", ".classpath", ".settings", etc. files for eclipse. This should be enough for eclipse to generate anything else necessary to build the project.
Any files/directories that are generated by eclipse during the build process (e.g. target & bin directory, *.class, *.war) should not be checked in -- they will be generated when needed during the build.
I am thinking that, since you are adding the necessary 3rd-party jars manually, these libraries might reside in a different path between computers (e.g. if the path contains the username, it will not be transferable to another computer for a different user). To fix that you can set up the classpath using an eclipse classpath variable. In Preferences->Java->Build Path->Classpath Variables set up a varable linked to the "root" folder where the 3rd party jars a stored. Then add the libraries to the project using this new variable, not their full path. To make it work on someone else's computer, you would only need to set this classpath variable to have the build path point to the correct libraries.
It might be beneficial if you migrated your project from eclipse only to a build manager (e.g. maven) that takes care of many of these issues for you. Eclipse can build a project from the configuration of the build manager, making it easier to manage the project.

Definining user libraries in Eclipse as part of a project and not a workspace

I saw that in Eclipse I can define User Libraries - to make setting the classpath easier (and probably for other reasons as well).
The problem is, that these libraries are only available in the workspace, and if I want other people using the same project to use them - I need to export my user library and they need to import it.
Is there any functionality like this on the project level? I basically need to have a 'classpath group' - can it be done?
If not, is there an automatic way to auto import the user library to the workspace when importing the project?
I'm using Eclipse 3.6.
JDT has the 2 concepts, user libraries and classpath variables. In the classpath variable, you can add jars to your project. Other team members have to fill in the variables in their workspace so their classpath is complete. This is useful when external jars might be in different locations on each team members local file system.
The USER_LIBRARY is a container for adding a logical group of local jars all at once. For example, the JRE_LIB container represents a number of local jars. But as you've seen, it points to a local set of jars meant to be used in multiple projects (as the JRE is added to multiple projects).
Aside from export/import (which you're already doing), I don't believe you can check CLASS_LIBRARIES into a project's SCM. If there was, the preference page would have a "Configure Project specific settings" link at the top.
Your best bet is to simply add the jars to the project, so they'll be included in the SCM. If they can be in different locations depending on the rest of your team, then use a classpath variable so it can be set in each workspace. That's the least amount of hassle as far as team members checking out the project and being ready to go.
The best way IMO is to use m2eclipse - Maven plugin for eclipse. In Maven all the dependencies are defined in pom.xml and downloaded automatically as needed. This means that the only thing you share with your team is pom.xml - your project definition.
There is a lot more advantages when using m2eclipse vs standard eclipse approch. More information is at http://www.sonatype.com/books/m2eclipse-book/reference/
The way I have used user libraries is for something like Ant. Define a user library "ant" for all the jars in ANT_HOME/lib. If including this in your Eclipse .classpath and then sharing with other users, they will get a build problem report until they create that "ant" user library themselves. It's useful, but you need to share knowledge on how to create the library. If you're using it for simple cases like above, then instructions for adding the right jars to the library are straightforward.
Another approach I've used is to build classpaths pointing into a folder (or folders) defined as a variable in Eclipse. See File -> New Folder -> Advanced -> Link to folder in the file system -> Variables. This lets you setup (again at workspace level) variable references to one or more folders. You can then build your Eclipse classpath/s with reference to the folder/s.
So say in your development environment, everyone needs to have a directory called "thirdparty" containing all the external jars dependencies (probably in hierarchy within that dir: thirdparty/apache; thirdparty/sun; ...). You define "thirdparty" as a variable pointing to wherever that dir is on your current system, you create a folder in your project/s using the variable. You can then setup (and share) classpath using paths into that folder.
It's similar to User Library and with similar limitations. The limitation is that the other users you share your project with must create variable folder/s as you have. But it's more flexible in that they don't have to add the jars explicitly as they do with a library; rather, your classpath/s in Eclipse point into the folder, as required for each project.
Note that although the folder variable is defined at workspace level, it can be reused in multiple projects, each of which builds their classpaths (.classpath files) with different references into the folder).
This is maybe something easier to show than to describe with words, but I hope it makes sense.

How to open existing Java 'solution' (not developed in Eclipse) using Eclipse?

I've been tasked with picking up someone elses Java code and adding some functionality to it.
I'ved pull down the source tree from CVS and see a bunch of .jar files in different folders. I'm guessing the developer did not use Eclipse.
I am new to Java (coming from .NET background) and have used Eclipse so far to create one Java project. I'm wondering now that I have this guys files (he has classpath.jar and other .jar files along with some subfolders each with 'java' files in them), how do I open them? I tried opening one at a time, etc.. but doesn't seem to work. IS tehre an easy way to do this? I thought there' might be some kind of 'import existing code' thing in Eclipse but I can find it. How can I do this? Do I re-create the folder structure and just add the existing files one a time?
Thanks much for any help
something like 'create project from existing source'?
http://www.stanford.edu/class/cs108/JavaTools/eclipse-guide/
if the existing code is not structured well, you are either going to have to heavily configure your project sources, or just change the project structure.
File -> new
Than select general->folder.
To make developing easier in eclipse i recommend some refactoring to the project.
create a new eclipse project using the parent folder as the home.
every folder that's the root of a hierarchy of java classes becomes a folder in the "source" tab (either on creation, or add through "project->properties").
every jar (at least the ones he's using, there may be extras) gets added in the project->properties libraries tab.
This is assuming that all of the hierarchies belong together and that the thing isn't structured to build little sub-projects out of pieces of the hierarchy. If there's a build file for this thing you might want to be sure that if the build file is doing that you're building things appropriately.
File->Import->General->Existing Projects into Workspace
OR
File->New->Java Project
This will create a sample java project for you. You can add the files appropriately.
Below is an example of a j2ee Project:
http://java.sun.com/blueprints/code/projectconventions.html
If C:\Workspace is the folder you are using as the workspace and you have your existing project placed as "C:\Workspace\ExistingProject"
Open Eclipse Got to File->New Project
Select the type of Project you want to create Use the name as "ExistingProject" for the project and click Finish or complete the remaining steps of project creation wizard normally.
Internally a .project file would be created in the ExistingProject folder and a .metadata folder would be generated under Workspace folder.
If you want to place the ExistingProject not under the workspace u follow the same steps.
There's 2 possibilities:
Import project from file system:
Create a blank Eclipse Project.
Then select File -> Import -> General -> File System. Select project, and point it to your created project.
Importing from CVS
Goto File -> Import -> CVS -> Project from CVS
Enter your CVS Host, Repository Path, Username and Password, and click next,....
Add what you need and click next (follow the instructions) until you're satisfied and click finish.
Hope this helps.
A simple tutorial that incorporates my 1st option and commmits it to CVS: http://thedesignspace.net/MT2archives/000662.html
Two options:
Maven - highly recommended but rather read this: http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html than have me re-write it here. Maven might seem like more effort up front but it pays for itself a hundred times over during the course of even a simple project.
Do it yourself (assuming Helios):
Move the source code Java files to ~/development/MyProject/src/java. Move the jars to ~/development/MyProject/resources.
In Eclipse, File > New > Java Project. Type in your project name.
Untick "Use default location" and browse to ~/development/MyProject.
Select src/java as your source folder (if Eclipse doesn't pick it up automatically).
Finish.
Then, for each error, you will need to find the corresponding JAR and add it as a library to your classpath in the project properties.
The important thing to bear in mind is that Eclipse is not like Visual Studio - you cannot easily just edit one file at a time and that is not what it is designed for. People can get frustrated with Eclipse after working with VS but if you just allow it to do things the way it wants you, your life will be much easier.
trick is finding the root folder. Generally, developers use the following:
project root
-- src
-- bin
at least, what's what Eclipse does by default. There are other ways it can be organized as Maven uses the following:
project root
-- src
-- -- main
-- -- -- java
etc...
More info on how Maven standardizes here:
That said, finding out how the source is organized shouldn't be too hard. Open up one of the .java files and look for the line at the top that starts with "package ". Should be something like this:
package com.somecompany.client.utils
Note, that's just an example, it won't be that exactly although it should start with "package". The value after package represents the path that the file should be in relative to the root of the source folder.
source
folder/com/somecompany/client/utils
So, if you follow the default way that Eclipse organizes things, it should look look like this:
project root
-- src
-- -- com
-- -- -- somecompany
-- -- -- -- client
... etc
SO, as other people have mentioned, you'll import from existing filesystem, point to the folder at the project root. You may need to configure it to point to "src" folder as a source folder. You may also need to import those .jar files into your project as well.
Good luck

How to set up multiple source folders within a single Eclipse project?

I have several somewhat separate programs, that conceptually can fit into a single project. However, I'm having trouble telling Eclipse to make several folders inside a project folder.
A simple form of the structure would be:
/UberProject
/UberProject/ProgramA/
/UberProject/ProgramA/com/pkg/NiftyReader.java
/UberProject/ProgramB/
/UberProject/ProgramB/com/pkg/NiftyWriter.java
That is, ProgramA and ProgramB are both projects (in fact, they're currently existing Java projects), which conceptually fit into UberProject.
I don't think I'm supposed to make UberProject be a Java project; it's not a classpath, for instance. ProgramA and ProgramB do seem like they should be Java projects (they might use different build dependencies as well), but I see no way in Eclipse 3.3 to create two folders under UberProject that are intended to contain Java code. I thought about adding a .project file to each of the two sub-projects, but I'm not sure that's appropriate, either. Eclipse help isn't being helpful, and I didn't see anything on SO about this specific problem.
Just to be clear: assume as given the necessity of the existence of UberProject. UberProject can be a Java project, or not; it doesn't matter. (Incidentally, it does contain other folders that do not contain Java code.)
There are probably several ways to do this:
1) UberProject is your JavaProject. Right click ProgramA -> Build Path -> Use as source folder. Right click ProgramB -> Build Path -> Use as source folder. Both ProgramA and ProgramB will do incremental builds to the same directory.
2) Two java projects (ProgramA and ProgramB). You can use UberProject as your eclipse workspace which would be easiest or you can use an outside workspace and import ProgramA and ProgramB as external projects.
There are probably other ways as well (maven multi-module project). Your choice probably depends on whether you have cyclic dependencies between projects. It should be relatively easy to try both 1 and 2 and see what works best for you.
You can have multiple source directories in a single project, but your description makes it sound like you want multiple sub-projects in a project, which eclipse doesn't allow (as far as I know).
If you really just want multiple source directories, where ProgramA, ProgramB, etc. contain only java files and nothing else, then you can do this relatively easy. Right-click on your project in the package explorer, and select Build Path -> Configure Build Path.... In the dialog that pops up, make sure Java Build Path is selected in the left pane, click the Source tab, then click the Add Folder... button. Pick ProgramA from the list, and repeat for ProgramB, etc.
If you want each source folder to have its own output folder, just check the Allow output folders for source folders checkbox and edit the output folders as desired.
If that is not, in fact, what you want to do, then another option might be to group your projects into a working set. A working set is not an UberProject, but it does help keep your projects organized in the package explorer.
Do you need UberProject? I have the same layout but have multiple top-level projects created with File|New project. If not, can you make it a General rather than Java project?
So you can do it via having two Java projects in your workspace.
Then the question is how to group the two projects together under "UberProject"
One way is to have an "UberProject" workspace, and switch workspaces between UberProjects.
An alternative is to define "UberProject" as a working set (Window:Working Sets) and add PrmgramA and ProgramB as projects of that working set. Select that working set, and you see only those projects.
You can have one java project, and define multiple source folders for it. That is normally do that for "main" vs "test" hierarchies within the same project.
There are ways, and ways. Pick one that works for you :-)

Categories

Resources