OSGi NoClassDefFoundError when running outside of Eclipse with my Bundle - eclipse

I have an application made up of multiple bundles. Inside eclipse it is working perfectly. When I export it as a product it was working perfect but something has changed and I can't work out what.
Now when I export my bundles, at runtime I get NoClassDefFoundError. The Error is in one of my bundle's activator and the class it is unable to find is in another bundle. A common bundle.
When I try to start the bundle I get the exception
Caused by: java.lang.NoClassDefFoundError: com/ardor/common/util/Systems
This class is in another one of my bundles com.ardor.common.
At the console when I inspect this bundle with the "bundle" command I get
Exported packages
com.ardor.common.util; version="0.0.0"[exported]
I'm stuck for ideas :(

When trying to remove compiler warnings I removed from the build.properties
output.. = bin/
with out this the bundle contains no class files
build.properties
source.. = src/
output.. = bin/
bin.includes = META-INF/,\
.

Related

how to package and reference text file in resources folder

I have a spark project using scala and sbt. At one point it references a text file which I want to be packaged.
This is how it is referenced in the application source:
getClass.getResource("/myFile.txt")
This works fine running the source code with sbt run. But I want it to be packaged and deployed to a server.
In build.sbt, after some googling I have got this to work
import NativePackagerHelper._
mappings in Universal ++= directory("src/main/resources")
adding this meant that the myFile.txt appears in the resources folder in the package. created using
sbt universal:packageBin
resulting folder structure:
target - universal - bin
- lib
- resources
however when I run my packaged application from bin/my-application.bat , I get the following error
Exception in thread "main" org.apache.spark.sql.AnalysisException: Path does not exist: file:/C:/Software/my-application-0.0.1/lib/my-application-0.0.1.jar!/myFile.txt;
Bear in mind I have zero experience of deploying scala or jvm based things so you may have to spoonfeed me the explanation
EDIT I later realised that the text file was in fact included in the .jar file.
the issue then was that getResource does not work in this case and I had to adapt my code to use getResourceAsStream
This can have multiple reasons.
Include files in your resulting jar
You added this line, which is not correct
import NativePackagerHelper._
mappings in Universal ++= directory("src/main/resources")
The src/main/resources directory is the resourceDirectory in Compile and the contents are always present in the package jar file (not the zip!). So I would highly recommend removing this snippet as you will have your files twice in your classpath.
The mappings in Universal (documentation link) define the content of the created package (with universal:packageBin the zip file). I assume that you are using the JavaAppPackaging plugin, which configures your entire build. By default all dependencies and your actual build artifact end up in the libs folder. Start scripts are being place in bin.
The start scripts also create a valid classpath, which includes all libraries in lib and nothing else by default.
TL;DR You simply put your files in src/main/resources and they will be available on the classpath.
How to find files on the classpath
You posted this snippet
getClass.getResource("/myFile.txt")
This will lookup a file called myFile.txt in the roots of your classpath. As in the comment suggested you should open your application jar file and find a text file myFile.txt at the root, otherwise it won't be found.
hope that helps,
Muki

Eclipse E4 application does not build correctly

I have an E4 application that contains two different plugins.
The second plugin holds all my shared models and those models are used in my first plugin.
When building and starting the application using Eclipse run functionality everything works fine. All the classes in the second plugin are found and use by the first plugin.
Now when I export the full application into an executable. The export itself doesn't give any errors but when starting my application it throws errors that the classes in the second plugin cannot be found. When I check the exported jar of the second plugin I can only find the source code in the jar, not the builded class files itself.
Are there some steps to also include the class files to the second plugin jar?
My build.properties of my second plugin looks like:
source.. = src/
bin.includes = META-INF/,\
libs/hibernate-c3p0-5.1.1.Final.jar,\
libs/hibernate-commons-annotations-5.0.1.Final.jar,\
libs/hibernate-core-5.1.1.Final.jar,\
libs/hibernate-java8-5.1.1.Final.jar,\
libs/hibernate-jpa-2.1-api-1.0.0.Final.jar,\
libs/lombok.jar,\
libs/logback-core-1.1.7.jar,\
libs/slf4j-api-1.7.21.jar,\
libs/jboss-logging-3.3.0.Final.jar,\
libs/jta-1.1.jar,\
libs/javassist-3.21.0-GA.jar
Assuming you have code in a source directory you need to have a . in the bin.includes to include that code in the build:
source.. = src/
bin.includes = META-INF/,\
.,\
libs/hibernate-c3p0-5.1.1.Final.jar,\
..... other libs ....
The build.properties is only used when doing the RCP build so you don't get an error for this when you run your RCP from within Eclipse.

IntelliJ build wrong JAR: Could not find or load main class

I have a simple example
public class FileSystemReadFile {
public static void main(String[] args) throws IOException {
System.out.println("Reading the file" + args[0]);
}
}
which is created in IntelliJ where I want to build JAR file; So what I did:
Added Artifact with dependencies (presumably I have some);
Ensure that MANIFEST.MF is located in src\main\resources\META-INF\ as it is already mentioned somewhere here on the site.
Run Artifact build which gave me JAR file in out folder and I run that jar file that said me "Could not find or load main class" java <name>.jar
You may see that main class is added into MANIFEST and location of manifest is also fine.
When I open that created JAR file, I see the same MANIFEST content, I see lots of dependency modules, but I don't see my class!
I suspect that is a cause. Any ideas?
If you include any signed JARs in your app and then use IntelliJ to build artifacts, it will extract the JARS and bundle them with your compiled output.
This then causes a JAVA security exception. I've seen this with Eclipse Paho and Bouncy Castle which are signed.
You can check if any of the library JARs you are using are signed using the jarsigner tool.
jarsigner -verify -verbose <path to library JAR>
Change your IntelliJ artifact setup so that these get bundled as libraries instead of being extracted. Extraction invalidates the certificate as you'd expect.
Try creating a dummy project with just Main. Add 1 library JAR (that you are trying to build with) at a time. Build an output JAR each time until Main breaks. That's how I found this.
IntelliJ should warn you.....
Not sure what was with IntellJ, but I rebuilded artifacts again and it was ok.
hadoop jar <Jar-name>
java -jar <Jar-name>
Everything is working fine.

Trouble running jar file created by Eclipse

I exported my Eclipse project as a runnable Jar file, added a manifest, as well as the appropriate class files with the command:
jar cfm JarFile.jar manifest.txt *.class
However, when I try to run the jar file with
java -jar JarFile.jar
I get the error that it "Could not find or load main class" etc. etc.
The structure of my manifest.txt file looks like this:
Main-Class: EclipseProjectName.src.packagename.mainclassname
(With a carriage return at the end)
Is something wrong with my manifest file? If not, what may be the reason that the main class cannot be found?
Thank you!
The manifest file extension should be .mf i.e manifest.mf. See here for jar file specification
Suggest to use fat jar eclipse plugin for exporting java projects as runnable jars.
Edit
Correct the content of manifest.txt as shown below
Main-Class: EclipseProjectName.src.packagename.mainclassname
project name and src are not required. Refer this
The entry in the META-INF/MANIFEST.MF entry in the finished jar file should be
Main-Class: packagename.mainclassname
and should correspond exactly to
/packagename/mainclassname.class
in your jar file (or jar file_s_ if you use Class-Path too).

"Package HttpClient does not exist" when compiling

I am attempting to compile EWSJavaAPI1.5 in Eclipse and in IntelliJ. I have had no luck. I keep getting a org.apache.commons.httpclient does not exist error. Its driving me nuts. I added the four required files:
commons-codec-1.4.jar
commons-httpcliient-3.1.jar
commons-logging-1.1.1.jar
jcifs-1.3.15.jar
The four jar files are being referenced, yet it does not find the httpclient when compiling. It may have to do with the class path but I am not sure if I am adding it to the class path correctly. What to do?
Check that the library classpath is set correctly and this library is added to the dependencies of your module. Another possible case could be corrupted jar file, try to download it again.