How to refer to another Java project from current project? - java

I used Eclipse to build a java application (Project A) which uses another (Project B) as reference. I just need to add Project B in Java Build path/Projects so i can run locally the main function from Project A.
Problem
When I deploy A to GAE, if A's doGet has B's classes, then GAE complains that it can't find it. Sure the war folder in A doesn't have anything that are in B.
Question
How do I let GAE deployment tool to upload all needed classes from B in order to run A?

You need to add project B as a library to Project A.
How to I let GAE deployment tool to upload all needed classes from B in order to run A?
If you add Project B as library to Project A, they automatically become one project,meaning where ever project A goes, B follows.
Try reading these:
Eclipse - Importing your own library
Android Library projects (this one shows you how to mark a project as library etc)

Related

Separate project or separate jar?

I have a question related to the way I should be setting up the overall architecture of my project. I have a project A with a set of classes in it. Now project B also needs certain classes which are actually present in project A. There are two approaches that I am able to think of:
Approach 1: Create a separate project C with the classes which are common between project A and project B. Include project C as dependency in both project A and project B build path.
Approach 2: While building project A, build a separate jar including only the common classes. Once this jar is built, include this jar in the build path of project B.
Could you please help me understand which is of these approaches is more standard and follows the best practices of setting up projects?

How to deploy web project on remote tomcat server outside Eclipse with .jar files in it

I have Rest API Project A which uses Project B and Project C for Database retrieval
Project B, Project c In which JPA is used for DB. Project A is working in Eclipse as i have added Project B and Project C's dependency at following places:
Properties -> Deployment Assembly--> Add -> Project B and C
Debug Configurations -> Classpath -->User Entries --> Add Projects B and C which includes all .jars of B and C automatically
Added project B and C to Projects--> Properties
Now i want my web project to run on Localhost without Eclipse.
I made ProjectA.war file and put it in tomcate's webapps folder and run tomcat.
It performs normal function like if i want to print Hello. it does..
But when i try to run any function which in turn call project B and C's function which has query to Database JPA its Not working.It doesn't return any data.
After Deploying .war in tomcat In Project A webapps\ProjectA\WEB-INF\lib only .jar which i added in Properties -> Deployment Assembly--> Add -> Project B and C Appears.
How can i make it work to use project B and C also. Please Help what .jars or dependencies should i also need to include and where??
You need to create jars for your project B & C an place them under the WEB-INF\lib of your project A in tomcat webapps directory.
Or if you don't create jars for your project B & C then copy the classes with proper package structures in WEB-INF/classes.
You can always check what is going wrong in Tomcat/logs directory.
A piece of advice:
Familarize yourself with use of build tools such as maven and ant. I would recommend you to learn about maven, its dependency management, and build plugins. You can really make your life easier with such tools.

How do I manage git submodule subprojects in Eclipse?

(In case it matters, these are Java Android projects)
Project A is a library project.
Project B depends on A. For ease of project management across the board for other people instead of just me, A is contained within B via a git submodule.
This means, if I want to make changes to both A and B, I will have the following:
Project A itself
Project B
B's submodule of A, let's call it A'. Same code, same project name, different location on disk.
Ideally I would like to be able to meet the following conditions:
Be able to modify A itself directly within Eclipse.
Be able to modify B and have it build, referencing A', all within Eclipse.
I don't really need A' as an imported project / no need to modify it, but I'd like it to automatically build its jar file and have that be referenced by B.
What I've tried:
Simply importing both A and A': Doesn't work, because Eclipse cannot import two projects with the same name. I can't simply change the name either because it's a submodule; would have to do that every git update, unacceptable.
Adding External Library reference to A', keeping it out of Eclipse entirely: actually not too bad of a solution, but requires at least one external build of it from the command line (using ant) before it will work...not the most convenient.
Doing a Source Link: Not the cleanest because A (and subsequently A') have multiple source folders within them, so I'd have to update each time I add or remove source folders within A. I'd rather just use the jar directly.
Is there a way to keep this all in Eclipse, or should I do something like #2 but with an added external build command to call ant on A'?
I have a similar situation. Import B as existing (android) project into eclipse after you git clone it. Make sure project A is initialized and updated (via git).
Now, do file->import existing project (android if A is an Android project) and go to where the submodule A project is inside of your project B and add it.
Now you have two separate projects in eclipse. You can change both projects, and it will all be under Project B, because that is where both projects are located.
Since this is specifically for Android, have you looked into Android libraries?
In my case I have a free (B) and a paid for (C) version of my app. Both share a common code base (A).
In eclipse I make A an Android library and both B and C use this library and all 3 are separate git repos. With this setup, changes I make in A are automatically available in B and C and I only need to maintain one code base for each project. No fancy build scripts/configs.
I think this sort of simplicity is what you were after? Apologies if I have misunderstood the question.
I'm not sure if i got your setup right:
Project A : src
+ /projecta.jar (binary of project A)
Project B : /src
+ /lib/Project A/src as submodule
+ /lib/projecta.jar (binary of project A)
..now you change A-A' and compile it, wanting to test it before pushing/commiting, right?
is it ok for you to just have the jar in B, or do you need the source within project B updated to A'?
first case:
have an ant file compile in project A and deploy it to B as well
that ant file is not part of project a, its just a local workaround
(its useful to have it in the ant view of eclipse)
Sketch:
<property name="projectb.libdir" value="..."/> set appropriate place in project B
<javac fromdir="./src" todir="./build" ...><fileset ... /></javac>
<jar /> from build to projecta.jar
<copy /> projecta.jar to projectb.libdir
Alternative is to create a .jardesc in eclipse, so you can run "create jar" on rightclick.
is that what you wanted to do?
Eclipse Mars Milestone 5 (4.5.0M5) has Multiproject-support. You only need to import "Existing Project" from Subfolder.
Just use a project build system to track your dependencies (that is what they are good for).
I would you suggest to use gradle (preferable IMHO but check which one fits better on your context) or maven, both can handle subprojects pretty well and can generate the eclipse configuration for you.

How do I run a main class in Eclipse from a different Maven project?

So I have say two projects, project A and project B. Project A contains a main class which I want to run. Project B contains some classpath resources which are required and also has a reference to Project A. If I run the main class in Project A directly, it will only run it in the context of that project and won't include the classpath resources as they're in Project B.
But if I try to run Project B, I can't select the main class (as it doesn't exist in that project) and if I try right-clicking the project -> Run As -> Java Application, it doesn't come up with that main class as a valid main class. (It does come up in the similar dialog for Project A.)
This seems to be a common problem from Eclipse; I found this related question but this person is not using Maven. Also, they accepted an answer which referenced project B from project A, which breaks the encapsulation I am trying to achieve here:
Eclipse 3,6 (Helios) referenced project has main method, cannot access from run config when targeting referencing project
So I got this working on
Eclipse IDE for Java Developers
Version: Kepler Service Release 1
Build id: 20130919-0819
#Michael Piefel - If B depends on A then A is already in B's run config classpath under the "Maven Dependencies" classpath entry.
The issue seems to be that the ui doesn't present the class to be chosen from its search dialog.
But if you just enter the fully qualified classname into the "Main class" text field and run it will still work. :D
Options I can think of:
Use the command line to kick off the main method passing the jar in the classpath.
Have project A refer to project B's config rather than the other way. [I realize you don't want to do this, but feel compelled to list it to be thorough)
Create a class in project B that has a main method which delegates to project A.
You really just need to tweak your Run Configurations accordingly. In the Classpath tab for the run configuration you use to run the class from project A, you can add project B as a dependency. Now while running that class, all the resources from project B are included.

importing project in Eclipse

Hello I am new to Eclipse (and I am a novice in Java): I am creating a project which should make use of some classes from another project. Do I have to export necessarily this last project as JAR file and add in my project? Are there other alternatives?
There are several alternatives:
If project A depends on project B, and both projects are in the same Eclipse workspace, you can just project B to project A's build path (project properties / build path / project). This has the nice advantage that it will (optionally) automatically pull in project B's JARs, plus updates to B will be used automatically by A, and you can debug into B's code (even using hot code replace).
If A and B are fairly separate, just make B into a JAR (or several ones), then add these to A's build path.
If you have the source of those class files you can just link that source
Or add the other project as a dependent project.
Or create a jar of those classes and add that in lib.

Categories