I'm trying to build an application that builds a resource file into a jar, but I'd like to have the project runnable within eclipse. I have a basic maven 2 structure for my project, and I'm unsure how to read in the file such that it's found and used when run from the JAR or from within eclipse. Thought?
Structure:
src/main/java
src/main/resources/file.txt
Current reading method:
getClass().getResourceAsStream("/file.txt")
Is there reading method that will pick up src/main/resources/*, as well as the root level of the JAR (where resources are deployed)?
I don't understand your problem. Resources from src/main/resources are automatically copied over to target/classes and are thus available on the classpath under Maven and Eclipse relatively to the root level at the same location (unless your Eclipse project is not properly configured).
And when packaged inside a JAR, the content of target/classes is packaged "as is" so nothing is changed.
In other words, accessing your file.txt like this is perfectly fine (and this is actually how things are documented):
// Retrieve resource
InputStream is = getClass().getResourceAsStream( "/file.txt" );
// Do something with the resource
...
If you have a problem somewhere, please clarify.
Update: I did a quick test with the maven-eclipse-plugin and I can't reproduce your problem:
$ mvn archetype:generate -DgroupId=com.stackoverflow -DartifactId=q2467362 -Dversion=1.0-SNAPSHOT
...
$ cd q2467362
$ mkdir -p src/main/resources
$ mvn eclipse:eclipse
...
$ cat .classpath
<classpath>
<classpathentry kind="src" path="src/test/java" output="target/test-classes" including="**/*.java"/>
<classpathentry kind="src" path="src/main/java" including="**/*.java"/>
<classpathentry kind="src" path="src/main/resources" excluding="**/*.java"/>
<classpathentry kind="output" path="target/classes"/>
<classpathentry kind="var" path="M2_REPO/junit/junit/3.8.1/junit-3.8.1.jar" sourcepath="M2_REPO/junit/junit/3.8.1/junit-3.8.1-sources.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
</classpath>
The directory src/main/resources is added as source folder as expected. Can you show your POM (especially the resources element if you define one)?
Anything that is put in src/main/resources using maven2 will be placed in the root level of the jar. So the method you are currently using will satisfy both conditions.
From the Maven page:
http://maven.apache.org/guides/getting-started/index.html#How_do_I_add_resources_to_my_JAR
Related
I have built a new machine and installed a fresh version of eclipse (Eclipse IDE for Java Developers 2021-06). If I run an old project it works. If I make a new project I the following error.
Exception in thread "main" java.lang.NoClassDefFoundError: net/crl/CRLibs/DBI
at EnvList.(EnvList.java:143)
Caused by: java.lang.ClassNotFoundException: net.crl.CRLibs.DBI
There are no errors in the code.
The build path looks like:
Line:143 is: static DBI db = new DBI();
DBI is defined in the crlibs jar.
In my old Eclipse (Oxygen) the build path looks like:
I tried adding the crlibs jar to the Classpath, but it will not let me save it (all boxes grayed out).
Now this code is copy of a template I use. The original runs just fine and its build path looks like the one from Oxygen.
If I comment out this line it will give the same error at the next library access.
It appears that the Class paths to the library are not being included.
Note The library was built with Oxygen. Do I need to rebuild it with the new version and if so will it then still work for all the code build with Oxygen.
How do I fix this?
Note:
Eclipse Java EE IDE for Web Developers.
Version: Oxygen.3a Release (4.7.3a)
Build id: 20180405-1200
And
Eclipse IDE for Java Developers (includes Incubating components)
Version: 2021-06 (4.20.0)
Build id: 20210612-2011
EDIT:
Here is the .classpath file.:
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="resources"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-16">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="E:/Documents and Settings/Cliff/libs/crlibs.jar" sourcepath="E:/Documents and Settings/Cliff/Libs_src/CRLibs/src/net/crl/CRLibs">
<attributes>
<attribute name="javadoc_location" value="file:/E:/Documents%20and%20Settings/Cliff/Libs_src/CRLibs/CRLibs/doc/"/>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="bin"/>
</classpath>
The path to the jar is there?
EDIT2
The command line:
In the figure below
You must delete the jar from Modulepath THEN add it to the Classpath. The JRE seems just fine in the Modulepath. Once my library is in the classpath the code works.
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.
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.
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.
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.