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.
Related
I can't find any info on how to call a different java file from a different folder anywhere.
Heres a diagram of what I wanna do
So for example
If the java file was in the same folder you would do:
HUD.HEALTH
but what would you do if the java file was in a different folder.
In Java you normally work in one folder, the so-called source folder. In this folder the classes can be divided into packages, usually using a certain pattern, for example com.company.something. In this case, a class is applied via import. The keyword import and the package name in which the class is located are specified. If they are external resources, the classes are included as external resources (.jar files), nowadays build tools like gradle and maven are used for this. Once the dependencies are imported, they can be used as well.
If you have two local projects, and want to access a class from project B in project A, for example, you work with the IDE. In Eclipse I would simply go to ProjectB's Build Path settings and add ProjectA. I recommend Intellij as IDE, there you solve it as follows:
Steps in IDEA ( You won't need these below steps if you follow below mentioned best practices):
Right click on project and select open module settings
Go to dependencies tab
click plus sign and add the module you want to use.
Best practices:
Never use project class in another project, always create a nice interface and use that interface in other projects.
If possible use Dependency Injection to manage different projects and their dependencies (this internally uses interfaces to do this).
Use build tools like Gradle and Maven to manage build process.
I have 2 projects (A & B) consisting of multiple modules which interact among each other. Let's say I debug a module of project A which at a later stage jumps into a module of project B. Intellij in this case uses a decompiled source code from the jar inside .m2 repo instead of the source files.
A possible workaround is to specifiy the sources when intellij shows the decompiled version but that is really tedious for multiple classes and modules.
How do I tell Intellij to use the sources?
Additional infos: Project A and B are in two separate folders and I have loaded Project A using its pom.xml. I manually use cmd to call 'mvn clean install'. Essentially, I need to specify an additional classpath to search for sources. At the moment the debug config is set to search the whole project but as project B is in another folder this is not possible.
I am not allowed to do changes to the pom.xml files inside each project and modules. I would also prefer to have both projects in separate folders.
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)
(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.
I have the following projects setup in Eclipse Indigo SR1:
Normal Java project (A) which includes package (P)
Plug-in project (B) which has a copy of the JAR that is exported from A and it then exports package P
Multiple other Plug-in projects (C...Z) which imports package P
What I want to do is get rid of the manual export and copy of the JAR from project A into project B. I do not want to convert this project to a plug-in project because the same project is used in a legacy application that does not use OSGi.
This is particularly annoying since when I update the JAR eclipse does not pickup the modified class files and all my breakpoints in there go crazy (a separate issue).
I thought I would be able to edit the project properties for B so that A was a "Required project" (Proeprties -> Java Build Path -> Projects), and since that essentially includes the source (at least I guess that is what the entry in .classpath is doing ) I figured I could then export package P from project B. However when I do this, eclipse just complains that plug-in project B does not contain package P.
I figured I could just link the source folders from A into B but that's not really what I want to do.
If a referenced project A is on the classpath then surely the plug-in project A should be able to export those packages, or am I wrong in this assumption?
What is the best way to achieve what I want?
Cheers,
Jason.
you should consider using maven as your build tool. the tool was built to automate exactly things you've described. Maven
You should convert your java project into a plugin project (an OSGi bundle) in this case. You can still use it both as a regular jar and as a plugin. When you convert it, make sure it creates the MANIFEST.MF. Just don't create an activator as that would require a dependency on org.eclipse.osgi, and don't add any other require-bundle dependencies.
An OSGi bundle is really just a jar (which is fine) with an OSGi MANIFEST.MF that provides information like what packages are exported, any dependencies, etc.