Eclipse Oxygen project linking to class files instead of source files - java

I have 3 Java projects in my workspace in Eclipse (Oxygen). The projects are A, B and Utils. The Utils project is used by both A and B. If I go into the Java Build Path for either of those projects, I can see the Utils project listed under the Projects tab option (required projects on the build path).
If I hold ctrl key and hover over functions, classes etc. that belong to Utils project from a source file in project A, then the links take me to the relevant source file in Utils project. If I do the same in project B, it always attempts to open the class file, not the source.
Is there something else I need to configure in project B to link the source files in Utils?

Make sure, in the Java Build Path of project B, in the tab Libraries, the bin/class folder of the Utils project is not added as a class folder. The dependency to the Utils project should be defined in the Projects tab only.

Related

How to add project instead of jar in vscode?

I have a gradle wrapper based java project A in which I am using jar of another project B.
I want to debug a code of jar project B while running A.
This was easy in Eclipse as we just need to add project B to current project A in workspace and adjust the import order.
Is there any way to achieve same in VSCode ?
Make project and Jar be stored in the same one folder then open the folder in VS Code. The folder can be a new workspace.
Reference: Workspace in VS Code

Intellij : import project as library for another project

I am developing a project A (Java Maven) which uses a library B (a JAR file)
I have the source code of library B, and I want to change code in project B, while it's used as library for project A.
Is that possible in Intellij to replace a JAR library by its source code ?
I'd configure a multi-module Maven project with the parent aggregate pom.xml and 2 sub-modules: for the app and for the library with their own pom.xml files.
Define a dependency on the lib module for the app module. IDE will resolve such dependencies via the sources and you will see the changes in the project instantly without the need to compile. Refactorings will also work across the modules so that you can rename a method from the usage in the app and it will automatically rename it in the lib.

Let's say a.java is in folder x, and b.java is in the parent folder, how would i code b.java to be able to access a.java in b.javas code?

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.

PDFBox import error in intellij

PDFbox jar is added in intellij(Setting> Project Structure> Modules> Dependencies) and Have added the gradle dependency as
testCompile 'org.apache.pdfbox:pdfbox:2.0.1'
in gradle build and the build is successful.Even after this importing 'import org.pdfbox...' in java file shows error as cannot resolve symbol pdfbox.
Also tried File->Invalidate Caches\Restart.
Sometimes, you don't need to open the Project Structure dialog to create a library and add it to dependencies of a module. This is the case when there are .jar files within your project content roots.
You can select such .jar files in the Project tool window and use these files to create a library. When doing so, you will be able to select the library level and the module to whose dependencies the new library should be added.
In a similar way, you can use a directory containing .jar files to create a library.
Open the Project tool window (e.g. View | Tool Windows | Project).
Select the .jar file or files to be included in the library, or a directory that contains the .jar files of interest.
Select Add as Library from the context menu.
In the Create Library dialog, specify the library name, level and the module in which this library will be used.
This solved the issue.

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