Setting up common classes for access from other projects - java

I'm trying to set up two Java projects to have access to classes in another Java project.
So far, the situation is visible in the following image:
(source: shrani.si)
However, I can't seem to import any classes from the Common project.
(source: shrani.si)
I could just have multiple packages in a single project, but as far as I know only 1 jar can be compiled per project.
Any help is greatly appreciated.

You need to add your "Common" project to libraries of project where you need to use classes from "Common" project.
For example right-click on "Clinet" project then "Properties"
In right menu choose "Libraries" and press button "Add Project..." and double-click on project "Common".
After that you can add import common.Item;
Also try to clean and build all your projects.

Related

Cannot import java classes under src/main/java into src/test/java in eclipse

I am using maven project in Eclipse, where I am not able to access classes under src/main/java in src/test/java class. I could use the classes in maven dependacies jars though. What am I missing ?
I have found that sometimes this error appears and by simply going Project -> Clean... the error will stop.
However if this does not work there is a blog with some more suggestions here
Updated 11/07/2017
The original link is no longer available, refer to web archive here
Or just have them all here..
‘Clean’ Your Eclipse Project: Go to Project > Clean in Eclipse [This seems to work for me]
Refresh your project folder (right click on your project > refresh)
Re-build your project
Clean your builds (If using Ant or Maven – clean your builds)
Recreate your project in Eclipse
‘Switch’ Workspace – then Switch back (Eg Change to Debug, then switch back to Java)
Remove and re-add your JRE:
Right Click on your project > properties
Click on the Libraries tab
Click on the JRE
Click remove, then OK
Repeat 1-3 again, but add the JRE again
You just need to import them. Eclipse should help you with this: if you try to use the class names, it'll suggest where to import from.
The test classes are a separate package, so the classes will need to be declared public.
Absolutely, one can do it..
First You need to navigate to Build Path-->Configure Build Path--> Source tab
then in the Source tab search/check [your Project Name]/src/main/java and change
"contains test sources" from No to Yes and save it.
This will resolved issue of import packages from "src/test/java" to "src/main/java" successfully
Project -> Clean
is enough for me to work

Is there a way to "import" from other on-disk Java projects in Eclipse?

I have two Java Projects in Eclipse, and I'd like to make one be able to access the classes in the other, without making it difficult to tell which classes came from which -- or, ideally, without having to list the classes in the accessing project. In other words, it looks like this (but with many more files):
Proj01
[package]
Useful.java
Proj02
[package]
CurrentProject.java
And I want to have some sort of equivalent of "import Proj01.Useful;" in CurrentProject.java. How would I do this?
Add the project as a dependency to the other.
Rightclick project -> Build Path -> Configure Build Path -> Projects Tab -> Add... -> Select the other project -> Click Ok.
You should now be able to import classes from the other project.
Essentially what you want to do is package 'Proj01' as a JAR file and include that JAR as a dependency in 'Proj02'. This will allow you to import the required class(es) in Proj02.
Personally, I'd recommend checking out Maven. Maven helps manage dependencies in your project and also the build process (which in your case can be used for building the necessary JAR file[s]).
Alternatively, if you're using a tool like Eclipse, you can use it to package up a JAR of your project.

Using Workspace for different projects

My new job requires to work with eclipse and the IDE works well so far. But there's one thing I couldn't manage / understand. I am used to Visual Studio and there's a solution file containing some projects and the IDE enables me to work with them.
In eclipse I have my workspace and can add my projects. My projects may have some dependencies to each other and so on. Works great. But how to manage more than one projects ( or in the VS term solution)?
Do I have to use different workspaces? Ok, I can do that, but what if there's one project I want to use in two or more projects.
It's a beginner question, but an important one I think. So it should be interesting for other .NET users as well. Thanks a lot for helping.
Björn
In eclipse you can add as many as projects you need based on your project dependencies.So now wen we are talking about the dependencies whether do you want add the .jar file or project.Both the ways will help you to get rid of compile errors in your project.
1.Add muiltiple projects just by importing your project
File -> Import -> Select type of your project (Suggested options or i
use : Existing projects into workspace ,Existing maven projects)
2.You can link different projects by
Right click the project in eclipse -> BuildPath ->Configure Build Path
-> Projects -> Add --> (locate your projects in the workspace).
3.You can package your existing project as .war or .jar and add them into your project by
Right click the project in eclipse -> BuildPath ->Configure Build Path
-> Select libraries --> Add external jars (Then locate your jar's and add them).
Basically 2nd option is used in the debugging purpose,while the 3rd is bit tough to debug.
All these projects can be navigated using Project explorer window.

Extracting Common Java Package

I have a project that Contains a bunch of java packages that I have recently understood are needed in a new project that I'm going to develop. I want these two projects to have the same code base so I don't have to update the common libraries in both. How can I achieve this in Exlipse?
my current set up is like this
Project1
CommonPackage
SpecifictProjectCode1
Project2
SpecifictProjectCode2
I want the following, I think:
Project1
CommonPackage
SpecifictProjectCode1
Project2
CommonPackage
SpecifictProjectCode2
CommonCodeProject
What is the best way to achieve this I understand I could extract a jar file or and include it in both project but I want to be able to debug the code and I also want to keep the code editable. Is it perhaps better to extract the code to a separate project and how do I go about doing that?
Any help is appreciated.
You can have three different projects in Eclipse: CommonCodeProject, Project1 and Project2. Then, Project1 and Project2 would have a dependency on CommonCodeProject.
To add a dependency on a project in Eclipse, go to Java Build Path and add the CommonCodeProject in the Projects tab.
Create 3 distinct projects:
Project1
Project2
CommonCodeProject
Go into the eclipse properties of Project1 > java build path >
choose the projects tab and add CommonCodeProject project to the dependencies
do the same for Project2
both project should now have as dependency your CommonCodeProject.
YES. Create third project(CommonCodeProject) as Java project and reference that new project in first two projects by navigation below:
Project1 -->Properties-->Java Build Path -->projects tab-> Add -->CommonCodeProject
Project2 -->Properties-->Java Build Path -->projects tab-> Add -->CommonCodeProject
This should serve your need.
if you would not be editing the common code then make it one project and import as a jar in the other two. or even if you are editing have 3 separate projects and build common code to create a jar which is used in other two projects
Aside from native Eclipse (as outlined in the other answers), I would use Maven+Eclipse with the m2e plugin (download this using the Marketplace). Depending on the complexity of your project, and it's application, you could even go as far as hosting something like Sonatype's Nexus with your common code deployed to as repository - this might be overkill though.

Netbeans - Add library that is currently being edited

I'm working with multiple related projects in Netbeans. Is is possible to add a library or reference to that library (made from building one of the projects) to the other projects, while I'm still editing that library? I can 'clean & build' that library everytime I edit it and then import it again to the other projects, but that would be a really tedious task. Is there a way I can do it conveniently in Netbeans? Thanks!
In the project properties -> libraries you can specify Jar/Folder, libraries or other projects. All changes in the pointing project will be available in your current project as you change it.
You still need to build it, but you don't need to import as Jar again.
When you right click on the library group in your project you can add another project as a library.

Categories

Resources