How can I make Java code accessible to two projects - java

A Java newbie here. I have created a small desktop application in IntelliJ IDEA 11.1. It consists of several java files with classes plus the main application file Main.java. I would like to make another application, which uses the same classes but a different Main file. I want to be able to change the classes source code from both projects. Therefore I'd like to have a structure like:
IdeaProjects\lib/myclasses\*.java (or similar with additional src if recommended)
IdeaProjects\project1\src\Main.java
IdeaProjects\project2\src\Main.java
How do I do it properly? I've read a lot about modules, libraries, jars and am just lost at the moment :(

I don't use IntelliJ so I can not help you specifically but You should create one project containing the shared resources and per program another project containing you Main files. Compile this first project into a jar file so you can use it as a library in the projects containing the main files.
In eclipse You can add other projects to your classPath. Maybe this is also possible in IntellyJ.

Related

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.

Can I create an Eclipse Project that produces multiple executable Jars?

I have a project with a library JAR and a set of applications. Right now the applications (all 5 of them) are in a single project. These tools each have one or two classes, they run from the command line with one exception. Even the GUI tool has only about 8 classes. I am struggling with how best to structure this in Eclipse.
I have seen that I can use maven-shade to create multiple executable JARs from one project. I would need to convert the project to Maven, but I assume that is not a big problem.
I can create a separate project for each tool. This seems like overkill to have several projects with one or two classes in each.
Additionally, both of these solutions will leave me with either a bare class file or a JAR plus the dependent library JAR. Not ideal for deployment.
Before I pick one of these, I thought I would ask here if there is a different approach that I am missing for packaging these tools.
In eclipse, Export --> Archive file, You can select/deselect what classes you want to export into your Jar File
Yes, but You could/need to create using Maven, and for each build building jar you need to set the main Class.
Checkout the documentation of Maven.
I think You could to the same with Gradle, but not sure.
https://maven.apache.org/plugins/maven-jar-plugin/examples/attached-jar.html

How to manage native shared library dependencies in separate Android Eclipse projects?

I have an android project in Eclipse which simply contains a "jni" directory containing c++ code which gets compiled into a native shared library, let's say libmod1.so. Then I have another android "front-end" project, which also contains a "jni" directory. I've set that project up to create a native shared library (let's say libwrapper.so) which I want to act as an interface to access libmod1.so, calling its functions etc.
I've seen a few questions about writing Android.mk files to express shared library dependencies but none I've seen refer to linking custom made libraries in separate Eclipse projects.
Rather than rely on a complicated make file though, if possible I'd like to instruct Eclipse to compile and copy the libmod1.so library into the "jni" directory of the "front-end" project whenever its built. From there I can compile it into the libwrapper.so library.
Does anyone know how to achieve this in Eclipse?
Or, can anyone suggest a better method of achieving the result I'm after? I'd prefer separate projects because I want to use the libmod1.so library with various "front-end" projects in future and so I want it to be in its own project.

Separate class paths for test code and main code

I have two source trees in my Java application in Eclipse. One is "src", and one is "test". Each one contains a directory tree of .java files. So far, so good.
However, I would like the classes in "test" to be able to see the classes in "src", but for the classes in "src" to not be able to see the classes in "test". Similarly, I want to include some test libraries. I don't want any of those libraries to be available in the "main" tree, lest I accidentally use one without thinking.
Is there an easy way to set this up in Eclipse?
There is no way to solve this in Eclipse without splitting the two source trees into separate projects (with one depending on the other). Maven could handle that at build time though.
You don't need separate projects if you use IntelliJ. You just specify application and test packages. It's possible to mark libraries as part of application and test as well.
Eclipse ought to have equivalent buried under all those plugins. If not, IntelliJ has a community edition now.
Or just use Ant. Personally, I prefer it to Maven. I find Maven to be too complex for such a simple job.

How to build a Java Servlet in Eclipse?

I'm new to Java. I have to modify this Web Servlet that is running on my Tomcat. The Webapps folder contains several files and directories, but only one .JAR file. I managed to decompile it using Java Decompiler JD-GUI, but when I create the project in Eclipse from the decompiled source and try to Validate it, Eclipse shows me around 389 errors. Errors like
HttpServlet cannot be resolved to a type
And I don't even know which Eclipse button rebuilds the .JAR file. Can anyone help me?
Your eclipse project will need to include the libraries that the servlet relies on to work.
If you create the Eclipse project as a Dynamic Web Project it will set up some of the basic structure needed, including the relevant libraries. You may need to include other libraries if the original code relies on them, but from your description it seems fairly unlikely.
You can create a JAR file from the Eclipse File Menu. click Export..., and select Java->Jar file.
Many projects will use ANT which allows you to define scripts to perform tasks needed to compile, package, deploy and test a project.
you create the Eclipse project as a Dynamic Web Project it will set up some of the basic structure needed, including the relevant libraries. You may need to include other libraries if the original code relies on them,
create a JAR file from the Eclipse
File Menu
click Export..., and select Java->Jar
file.

Categories