Eclipse: Package multiple projects into one JAR - eclipse

I have multiple projects but only one with a Main class. The one with the Main class depends on the other projects. These projects are referenced in Eclipse, but when I export my JAR, the other projects are not exported with that JAR.
How can I export my Main project and "include" the other projects into that same JAR? I'd rather not have several JARs and have to define them into my Classpath on the command line.

Don't do it the hard way. Use Eclipse's own exporter. First ensure that you've the other projects referenced as Projects in the main project's Build Path. Once done that, just rightclick the main project, choose Export and then Java > Runnable JAR file. Choose the launch configuration (which you used to test the main() class locally) and then you've 3 Library Handling options to package the JAR:
The first option will just repackage the classes of other projects inside the JAR. Everything is plain thrown together.
The second option will copy other projects as JARs inside the JAR. This does normally not work that way, but Eclipse also adds a special launcher which basically copies the embedded JARs into memory, extracts there, adds the files to the classloader and then invoke the main() with that classloader.
The third option is something you don't want for this particular case.

If I were doing it I'd 'mavenize' the projects, using a maven dependency for each project dependency and then use
mvn assembly:assembly
see http://maven.apache.org/plugins/maven-assembly-plugin/ for more on this plugin

Related

Include one project's jar libraries in another project depending on the first

I am working on two projects in Eclipse.
Project A depends on some jar files that come with the project, and those jar files have been added to the “Libraries” tab in Project A's “Java Build Path” property in Eclipse.
Project B depends on Project A, as well as directly using some classes in some of those jar files in Project A's build path.
I had assumed that adding Project A to Project B's Java Build Class would also add the jars already in Project A's build path, but that appears not to be the case.
Do I have to manually add those jars to Project B's build class, or am I overlooking a setting? If so, why is that a useful standard behaviour?
You have to manually add the jars to project B's classpath.
Adding a project dependency means project A depends on the compiled output of project B. Project B's output (its compiled .class files) doesn't contain the .jar files it depends on.
Why is this? I don't know the rationale of the eclipse authors, but my guess is that they want to keep the classpath as simple and verbose as possible. Things can get confusing if you have multiple versions of the same library on the classpath.
In vanilla java you can provide directory names for the classpath. When loading a class, the JVM will search these directories in order. Eclipse encourages a stricter approach where each jar is specified manually. Note that you can add multiple jars at once, so it's trivial to add all of project B's jars to project A's classpath.

class test in an existing project j2ee converted to maven project

I'm a beginner in Maven and i have to "mavenize" an existing project for a company.
I opened the project in Eclipse and right click on the project -> Configure -> Convert to Maven Project.
So after that I didn't get the folders : src/main/java, src/main/resources, src/test/java, src/test/resources. I didn't get anything.
I have the m2e plugin and also Maven 3.3.3
I get the lib and Maven Dependency and arrange the pom.xml but i didn't have any test folder and any class Test so when running mvn:test i get no tests to run.
My question is : I have to create all the classTest manually ? Or Maven is supposed to create them automatically ?
Convert to Maven would normally:
configure the Maven Compile Plugin according to the JDK version in use by the original project
configure the build/sourceDirectory element (normally to src) instead of the standard src/main/java
ignore the bin folder used by default by Eclipse (so may see it in the Package Explorer and you can safely delete it)
Some manual steps are required (and suggested):
move the code to a newly created src/main/java folder and remove the build/sourceDirectory customization
remove the bin directory (it will now be target/classes)
Create src/main/resources folder and move any required resource (configuration file, etc.) to this folder
place your test sources under src/test/java, which you also need to create
if not the case, rename any test class to end by the *Test or *TestCase suffix so that by default the Maven Surefire Plugin will pick them up automatically (if not possible, alternatively you would need to configure it as described via official documentation here
If you don't have any test class yet, of course Maven would never create them automatically, you are responsible of writing your test cases, Maven would only run them.

Diffrence between importing an existing project into Eclipse VS adding a dependency

I need you help understanding nub stuff
What is the Difference between
1. Importing and existing project into Eclipse
2. Adding a dependency ?
The "difference" is that those two are completely different things.
If you import projects into the workspace, you can edit them. For example, you could create a project with Maven, generate Eclipse project files and then import it into Eclipse, or checkout an existing project from SVN or Git am import that one. You can not edit individual files in Eclipse; everything has to be part of a project.
If you add a dependency, you can use the things defined in that dependency in another project. Usually, that dependency would be a JAR file. You can add individual JAR files manually, or use other Programs like Maven aggregate the dependencies and add them to the project.
Also, you can add a project from the workspace as a dependency to another project (after importing both into the workspace). Compared to adding a JAR file as a dependency, this has the advantage that you can edit the one project and the changes will imediately be reflected in the second project that depends on the first one. This makes development much easier than generating a new JAR file from that project every time something changes.

get Eclipse to export libraries as a jar

How can I get Eclipse to export as a non-runnable jar all the contents of JRE System Library [JavaSE-1.6] and Referenced Libraries?
I want to use -classpath to bring together several jar files rather than use Eclipse's Export > Runnable JAR file. Motivation: swapping out a single class that happens to be in a package of its own, by swapping the jar.
It's easy enough to export my own packages in (non-runnable) jars but now I need the "library" classes as well and I have not found an easy and obvious way to do that.
There is an option when you export a runnable JAR to "Copy required libraries into a sub-folder next to the generated JAR". Would that work for your case?

Adding jars to a Eclipse PlugIn

I try to build a Eclipse plugin that has to use a self written jar which is dependent on other jars, but I don't get the point where to start with handling jars as seperate PlugIns. Anywhere I have to use just the .jar files or am I wrong?
I think I found a proper solution; the trick is that you have to implement all the files via Eclipse. I just copy here the solution which was posted to news.eclipse.platform:
Include the jars in a plugin:
Use Import > File System to import the jar files into your plugin project, say in the <project>/lib directory.
Use Add... button to add the jars to the classpath section of the plugin.xml > Runtime tab.
Use New... button to add "." library back (with no quotes, of course).
Make sure your binary build exports the new jar files on the plugin.xml > Build tab.
Save
On the project, use context menu > PDE Tools > Update Classpath to correctly add the jars to the eclipse project classpath.
What is a self-written jar?
Normally you turn 3rd party jars into bundles using an OSGi MANIFEST.MF (See New>Plug-in Development>Plug-in from Existing JAR archive) or you include them in your plugin.jar and add extra Bundle-ClassPath entries as mentioned by TomaC.
If you mean at runtime your plugin will create a new jar and needs to load it, that's different, though.
Project Properties -> Java Build Path -> Add External jars. Is this what you are looking for?