How to add new jar file to existing deployed jar in server - java

I have a RMI application which is running on the server. Now i want to add external jar file to an existing deployed jar.
Note: It is not web application.
For example, here i mention the jar directories
MyApplication.jar
manifest.mf
.classpath
classes
My java class files
lib
test.jar
test1.jar
images
META-INF
MANIFEST.MF
.classpath has
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="lib" path="lib/test.jar"/>
<classpathentry kind="lib" path="lib/test1.jar"/>
<classpathentry kind="output" path="classes"/>
</classpath>
Now i want to add test2.jar to my lib folder inside MyApplication.jar and updating .classpath file.
Is it possible to add it?

If you cannot use JRebel then you will probably have to try to write your own class loader to achieve this. There are many pitfalls. See this answer and this question for some pointers.

Related

Sublime Text 3 and Java with language server jdt-ls

I have downloaded the jdt-ls as language server and configured as described here https://lsp.readthedocs.io/en/latest/#java
My project has source code which is in 'src/main' but sublime text (or jdt-ls) seems to assume that the source code lies in the root folder, i.e. every java file has a "wrong" package name. Is there a configuration for projects?
I found the solution. In the root of the project there must exist a .classpath file. Here is my file for example:
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src/main" output="bin/src/main"/>
<classpathentry kind="src" path="src/test" output="bin/src/test"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
...
</classpath>

Can I select different .jar files in an XML .classpath file depending on what OS I am building for?

We are building a Java tool that runs on both Windows and Linux. The project is built in Eclipse. In the project we include The Standard Widget Toolkit (swt.jar). There are no common swt.jar file for both Windows and Linux, so we have to use different files, and this is what is causing the problem. Some developers are building on Windows and some on Linux, and to make it work the developer have to change the swt file to match the current building environment and this has to be done in a number of places. This is one of the .classpath files:
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry combineaccessrules="false" kind="src" path="/logviewer_common"/>
<classpathentry kind="lib" path="/logviewer/swt/linux_x64/swt.jar"/>
<classpathentry kind="con" path="SWT_CONTAINER/PLATFORM"/>
<classpathentry kind="output" path="bin"/>
</classpath>
Is there a way to add a condition that if I am building on Windows the file /logviewer/swt/win64/swt.jar is included and if I am building on Linux instead the file /logviewer/swt/linux_x64/swt.jar is included?
I have been trying to read and understand the XML tags and found that there is an <os> tag where one can specify Windows or Linux(unix), but it seems like I am not clever enough to understand if or how this can be used.
It is configured and built with Ant.
Would be very grateful for some help!

How is .classpath file in eclipse generated?

I've a java project in the eclipse which has a .classpath file as shown below:
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry including="**/*.java" kind="src" output="target/test-classes" path="src/test/java"/>
<classpathentry excluding="**/*.java" kind="src" output="target/test-classes" path="src/test/resources"/>
<classpathentry including="**/*.java" kind="src" path="src/main/java"/>
<classpathentry kind="src" path="src/generated/java"/>
<classpathentry excluding="**/*.java" kind="src" path="src/main/resources"/>
.......
<classpathentry kind="output" path="target/classes"/>
How does this file gets generated? Can I edit this file?
I want a certificate file to be added to the project in path target/test-classes. Where do I configure that?
My understanding is the target/test-classes folder gets generated after the mvn clean install. So I think there should be some place where I can configure what all I need in the target folder. Do I need to configure it in pom.xml ?
The .classpath file reflects the content of all the settings that you apply manually to your BUILD PATH setup within your project.
In other words: while using the eclipse UI to setup the BUILD PATH, all that information goes into the .classpath file.
And it is perfectly possible to stop eclipse and make changes to that file within your preferred editor; then restart eclipse, probably do a full refresh; and (unless you messed up) the changes should be visible when opening your BUILD PATH settings again.

How export Java jar from Eclipse with dependencies on other jars?

I am trying to create a self-contained .jar file from an Eclipse project. By "self-contained" I mean that all dependenies of this project (i.e. other jars) should be included in the generated jar. I would like to use the exported jar in another project as a library.
The problem is that I get a NoClassDefFoundError for one of the classes from the dependencies. I have tried exporting both a runnable jar with all dependencies packaged into it and a "normal" jar. Neither of these works.
The manifest looks like this:
Manifest-Version: 1.0
Main-Class: com.path.to.MyMainClass
Class-Path: lib/resolver.jar lib/xercesImpl.jar lib/xml-apis.jar lib/Mac-Cocoa64/swt.jar
The .classpath look like this:
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="lib" path="lib/resolver.jar"/>
<classpathentry kind="lib" path="lib/xercesImpl.jar"/>
<classpathentry kind="lib" path="lib/xml-apis.jar"/>
<classpathentry kind="lib" path="lib/Mac-Cocoa64/swt.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
The missing class def is from the SWT.jar file.
Any idea what might be wrong or what steps I need to take in order to successfully export this jar so that it can be included elsewhere?
Cheers,
Martin
It may help someone
1.Select Export from Project Properties
2.Select Runnable Jar option
3.Finish the JAR creation (Here, you have the option to package all dependencies into the generated jar.)
Just to answer this question: In the end, we decided to include the jars of the dependencies directly in the project and avoid havign jars in jars. Using Mavin is also being considered, but would require some substantial chagnes to the way our projects are setup.

Can a java .classpath refer to a .jar file within the eclipse plugin folder independant of install location?

I have an Eclipse plugin which stores its class library at \plugins\edu.wpi.first.javadev.sunspotfrcsdk_1.0.6.5\sunspotfrcsdk\lib\WPILibJ\classes.jar within the Eclipse install directory. At present, the .classpath uses an absolute path, so breaks when the project is run on another computer.
How can I make the .classpath refer to a .jar file using a path relative to the eclipse install directory?
Plugins should generally be referenced in eclipse .classpath as Libraries not Jars.
if you look at the .classpath file you'll see this distinction:
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="test"/>
<classpathentry kind="src" path="webdriver-tests"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/>
<classpathentry kind="con" path="org.testng.TESTNG_CONTAINER"/>
<classpathentry kind="lib" path="lib-new/test/hamcrest-all.jar"/>
The kind src is a source folder the kind con is a Library, and kind lib is regular jar.
You'll notice the kind="con" points to a static container for the library and is not dependent on a path. Plugins should provide this container and you can add them to your build path through project context menu
Build Path->Configure Build Path...->Add Library
Here's a screenshot:
Note that all developers will need the plugin installed for this to work.

Categories