How to import packages from a project to another - java

I have two projects (let say project A and project B) the two works with Spring( The same version)the only difference is that the first one has an Eclipse IDE and the second one STS and I want to use and work with the project A in the project B.
I've tried to manually copy/past a number of classes but the classes are so much dependent between them it makes it impossible.
I expect to show me how to import/export project A and use it in project B

Basically project A should be in your build path. Any of the two ways works
Export your project A as jar and add the jar to your Project B's build path.
Projects BuildPath > Projects > Add your project A.

Basically the one above me is really close , but If you will export project A to jar it might be not work if you want use it on server (Tomcat for example).
So you can make it "Runnable jar" from the first place and then as he said :
project B (right click) >>>> build path >>>> configuration path >>> libraries >>> add external jar >> and browser when you saved the jar.
now you can make instance of projects A classes and the functions already inheritance .
glad it helps you :)

Related

Eclipse I can't link project B to project A

I work with Eclipse. I have two projects in the workspace. This is my Eclipse navigator.
I want to add AR04.CME to cmeWeb.
Folder cmeShare has some jar which I need in cmeWeb.
I click in cmeWeb properties ->Java Build Path -> Projects
This is the screen
AR04.CME doesn't appear as project.
How can I add this project?
Linking Project A to another Project B is useful when you have source code in A that you want to share to B. That's not your case. You don't seem to share source code, but a compiled library instead.
Your best option is to edit cmeWeb's Build Classpath > libraries > add jar > select your Jar library.

Using Custom Packages in Multiple Projects

I've seen different packages in source code such as com.website.package These packages are used across multiple applications, and I've been trying to accomplish something similar.
The only way I know of to achieve using the same packages in different projects is to copy each source file into the new project which would create the new packages. this probably isn't the preferred method, as it could possibly drag thousands of files into the project (see Java's library).
How would this be achieved?
TL;DR: How can I use a custom written package in multiple projects without copying many files? (aka Java's libraries).
Take the class files and create a jar. Put the jar on the classpath of all the applications that need those classes.
How do you do that?
Inside an IDE, you can create multiple projects and put the shared java code in one project. Make the other projects depend on that project and you can share things within the IDE. That means you run inside the IDE and it will use the shared code.
For example, in Eclipse, you choose the project that will use the shared code/project. Right click on it in the package view or navigator view and choose "properties". Select the option to set the build path and there is a tab for selecting projects that this project depends on. Select the shared project and then it's code is callable from that project.
In this case, any change you make to the shared project will be immediately available, inside the IDE, to those projects that depend on it. Run them right then and the change will be in effect.
For running outside the IDE ...
If you are using Eclipse (or some other IDE) it will have an option for creating a jar. In Eclipse you create a java project and move the classes you want to be in the library/jar into this project. Then, once you have all the right classes, including those that others depend on, you will do some sort of build to create the jar.
Eclipse has an 'export' option on the 'file' menu. Use that and select to export as a "java" > "jar" and then select the project you just made.
You can also create a Maven project of type "jar" into which you put all those classes (as java files) and then building that project with Maven will create the jar. The "install" goal for Maven will deploy it to your local repository.
Any time your shared code is shared by way of a jar, you will have to rebuild the jar and copy it into the location(s) from which it is shared by other projects before changes inside the jar take effect.
Maybe export the package to some place on the drive and create linked folders to it in the projects?

add other projects as library in netbeans

I have a project A (for example an Ant based Java Application project) and a second project B (a Java Class Library project or another Java Application project, both Ant build based.)
Project B has some additional Java library dependencies (.jar files in the Libraries folder).
A depends on B. A can use classes which are written by myself in B but it can not see classes in the additional libraries of B unless I add those libraries/jar files to A.
Is there a way to get NetBeans to automatically manage the library dependencies in A by including the dependencies of B in addition to B's own jar file? In eclipse when you add .jar files to project B, it is not necessary to add them to A again.
Thanks.
In your project folder you should see a folder called Dependencies, right click on that folder and choose Add Dependency.... Open the Open Projects tab; from there you can select your project from the list. Click Add.
This works for NetBeans IDE 7.2.1.
The simpler NetBeans Ant project Libraries folder is used for compile time and run time dependencies of each individual project. If your "Java Class Library" type Ant based project B has additional library dependencies, they are not automatically forwarded or updated in Project A when you include B as a library of A.
When project A is built, it's .jar file will only contain Project A code and the lib/ folder will only include .jar files from it's Libraries folder. This can cause confusion when you upgrade the dependent library versions and possibly forget to upgrade them everywhere you depend on project B.
You will need to manually manage the dependencies unless you switch to a project management and build system that handles this, like Maven.
A NetBeans Maven project will have a Dependencies folder instead of a Libraries folder and if project B is properly setup and registered as a Maven project and project A is also created as a Maven project, you could Add Dependency on B to A and B would forward it's dependency information to Maven project A.
If you want to add another project as a library in netbeans right click on the libraries directory and select "Add Project", select the project you want to add. When you do this the jar file(in the dist dir) that has been built for the project you are adding will be selected and then click the "Add Project jar files" and that should add the jar file to your project. Alternatively you could select "Add Jar" instead and add the jar file yourself the way that Ravi described.
In your project folder you see a folder called Libraries , right click on that folder from there you can select your .jar file. You need not to add .jar files repeatedly after adding once to your project. You just need to Clean and Build your project whenever new files are added to .jar file.

Eclipse 3.6 Tomcat does not put jars from deployment assembly to it's deployment tmp dir

The are two "kinds" of jars in my project. One - jars made from projects which ,my project is depend from (e.g. my-second-project.jar), another - jars which are third-party libraries (e.g. hibernate.jar). The second kind of jars DOES NOT appear in deployment tmp dir while the first DOES.
I'm using eclipse 3.6, my project is dynamic web project, my tomcat version is 6.0.28. I've tried to recteate it - it doesn't help. I've also got similar web-project which works well.
I've made temporary work-around - I've put my jars directly to classpath. But this is just work-arond, but not the problem reason.
My waring/errors perspective output is clean, no connected messages whith this problem. Also I've got another "interesting" problem which might be connected whith this one. It's described here
Any help would be appreciated cause I've tried everything I can and I realy don't know what to do.
Do you have two projects, one depending on the other? And you don't get the jars from the project that the other project depends on? If so, make sure the jar files are exported from the first project: right click on the project, Build Path -> Configure Build Path... and then go to the "Order and Export" tab and tick the jar files you want to export.

Eclipse: Is there a way to create dependencies between source folders?

I have a Web Project with two source folders in Eclipse. Folder A, depends on Folder B being compiled. Is there a way to create this dependency in Eclipse, without having to create separate projects for each folder? I know projects can be set up to depend on one another, but I am not looking to do that.
The eclipse compiler automatically manages dependencies at the source level within all source folders of a project, you don't have to do anything special.
Edit: You answer "you would not be able to have Folder B reference classes in Folder A" to a comment - that's kinda the opposite of a dependency. And no, I'm pretty sure you cannot enforce that within a single eclipse project.
You can create a ant task to do that for you
You can have two programs in the same Eclipse project, with two separate main methods. You can then make two separate Run Configurations.
Before running you can then build the project. Building the project will automatically build both programs in the project, so you wont have to remember to manually build your second program.

Categories

Resources