I've generated classes with XMLBeans from an xsd file and packed them in a jar file. then I've added that jar to the project classpath in eclipse and everything compiles and runs fine.
I built a stand alone jar file from my project with Maven and again the build is successful, but when i try running it i get this error:
Exception in thread "main" java.lang.ExceptionInInitializerError
at com.oblicore.oblisync.resolutions.TestsDocument$Factory.parse(TestsDo
cument.java:126)
at com.oblicore.oblisync.handlers.TransferEntitiesHandler.getResolution(
TransferEntitiesHandler.java:117)
at com.oblicore.oblisync.handlers.TransferEntitiesHandler.resolveConflic
ts(TransferEntitiesHandler.java:103)
at com.oblicore.oblisync.main.Orchestrator.run(Orchestrator.java:107)
at com.oblicore.oblisync.main.Orchestrator.main(Orchestrator.java:58)
Caused by: java.lang.RuntimeException: Cannot load SchemaTypeSystem. Unable to l
oad class with name schemaorg_apache_xmlbeans.system.s8B21CFFFCFED0B2438C2585C61
F113F7.TypeSystemHolder. Make sure the generated binary files are on the classpa
th.
at org.apache.xmlbeans.XmlBeans.typeSystemForClassLoader(XmlBeans.java:7
83)
at com.oblicore.oblisync.resolutions.TestsDocument.<clinit>(TestsDocumen
t.java:19)
... 5 more
Caused by: java.lang.ClassNotFoundException: schemaorg_apache_xmlbeans.system.s8
B21CFFFCFED0B2438C2585C61F113F7.TypeSystemHolder
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:303)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
at org.apache.xmlbeans.XmlBeans.typeSystemForClassLoader(XmlBeans.java:7
69)
... 6 more
The missing class is in the jar i created with XmlBeans, how do i tell maven to add it to the jar it creates from my project?
While doing WSDL2Java a directory named resources will be created. Copy the schemaorg_apache_xmlbeans which presents under resources to classpath of your project. This should be the fix.
In your generated jar file make sure you have included the class files generated from your xmlbeans.
From the stacktrace :
Caused by: java.lang.ClassNotFoundException: schemaorg_apache_xmlbeans.system.s8
B21CFFFCFED0B2438C2585C61F113F7.TypeSystemHolder
it suggests that during compile time the required class files are in classpath but in your built jar these files are missing.
Check your jar file to see if these classes are present.
EDIT: As per question rephrased
For building jar with dependecies in Maven use jar-with-dependencies option, example
Two very good reference :
http://www.sonatype.com/books/mvnref-book/reference/assemblies-sect-basics.html
http://thomassundberg.wordpress.com/2011/03/05/create-an-executable-jar-from-maven/
In the second one you don't need a main class if your jar is not an executable jar
Please add below tag in pom.xml. Error wil go
<!--
this tells maven to copy the openejb-javaagent jar into your target/
directory
-->
<!-- where surefire can see it -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<id>copy</id>
<phase>process-resources</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.apache.openejb</groupId>
<artifactId>openejb-javaagent</artifactId>
<version>3.0-beta-2</version>
<outputDirectory>${project.build.directory}</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>target/generated-sources/axis2/wsdl2code/resources</directory>
</resource>
<resource>
<directory>target/generated-sources/xmlbeans/resources</directory>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
When you have this kind of error
The TypeSystemHolder.class generated by WSDL2Java is not be placed in your classpath in order to avoid this error.
Please copy TypeSystemHolder.class from "resource/schemaorg_apache_xmlbeans/system/s68C41DB812F52C975439BA10FE4FEE54" folder.
And Paste TypeSystemHolder.class file into your classpath folder (build/classes/schemaorg_apache_xmlbeans/system/s68C41DB812F52C975439BA10FE4FEE54) folder
Extract jar in which you want to include schemaorg_apache_xmlbeans folder.
Copy schemaorg_apache_xmlbeans folder in extracted folder (result from jar extraction).
open command prompt in extracted folder.
make jar again using jar cf command. e.g jar cf test.jar *,
to include all folders.
Deploy that jar .
I meet this problem. In our project we use customize classloader to compatible poi old version. when parse '.xlsx' file ,It's occurs the same error. follow is the origin code
private static String EXCEL_PATH = "com.alibaba.excel";
private static String POI_PATH = "org.apache.poi";
private static String OOXML_PATH="org.openxmlformats.schemas";
private static String MICRO_OOXML_PATH="com.microsoft.schemas";
private static String SCHEMAORG_APACHE_XMLBEANS="schemaorg_apache_xmlbeans";
#Override
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
if (name.startsWith(POI_PATH)
|| name.startsWith(EXCEL_PATH)
|| name.startsWith(OOXML_PATH)
|| name.startsWith(MICRO_OOXML_PATH)
) {
synchronized (getClassLoadingLock(name)) {
// First, check if the class has already been loaded
Class<?> c = findLoadedClass(name);
if (c == null) {
c = findClass(name);
if (c == null) {
throw new ClassNotFoundException("custom classloader can't find this class");
}
}
if (resolve) {
resolveClass(c);
}
return c;
}
} else {
return oldClassLoader.loadClass(name);
}
}
we use customize classloader to load ooxml-schemes.jar,but schemaorg_apache_xmlbeans.system.s8
B21CFFFCFED0B2438C2585C61F113F7.TypeSystemHolder is not in our classloader load path ,default classloader load it .
so i solved it by change my classloader code
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
*// we change here to load schemaorg...*
if (name.startsWith("schemaorg_apache_xmlbeans")
) {
//todo
}
//todo
}
to loader this class ,then it's be ok
Related
Hello I'm using a configuration file from src/main/resources in my java application. I'm reading it in my Class like this :
new BufferedReader(new FileReader(new File("src/main/resources/config.txt")));
So now I'm building this with maven using mvn assembly:assembly. Here is the bit for that in my pom.xml :
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.3</version>
<configuration>
<finalName>TestSuite</finalName>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.some.package.Test</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
So when I run my app I get this error :
src\main\resources\config.txt (The system cannot find the path specified)
But when I right click on my assembled jar I can see it inside, anyone knows what I'm doing wrong?
Resources from src/main/resources will be put onto the root of the classpath, so you'll need to get the resource as:
new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/config.txt")));
You can verify by looking at the JAR/WAR file produced by maven as you'll find config.txt in the root of your archive.
FileReader reads from files on the file system.
Perhaps you intended to use something like this to load a file from the class path
// this will look in src/main/resources before building and myjar.jar! after building.
InputStream is = MyClass.class.getClassloader()
.getResourceAsStream("config.txt");
Or you could extract the file from the jar before reading it.
The resources you put in src/main/resources will be copied during the build process to target/classes which can be accessed using:
...this.getClass().getResourceAsStream("/config.txt");
Once after we build the jar will have the resource files under BOOT-INF/classes or target/classes folder, which is in classpath, use the below method and pass the file under the src/main/resources as method call getAbsolutePath("certs/uat_staging_private.ppk"), even we can place this method in Utility class and the calling Thread instance will be taken to load the ClassLoader to get the resource from class path.
public String getAbsolutePath(String fileName) throws IOException {
return Thread.currentThread().getContextClassLoader().getResource(fileName).getFile();
}
we can add the below tag to tag in pom.xml to include these resource files to build target/classes folder
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.ppk</include>
</includes>
</resource>
</resources>
I think assembly plugin puts the file on class path. The location will be different in in the JAR than you see on disk. Unpack the resulting JAR and look where the file is located there.
You can replace the src/main/resources/ directly by classpath:
So for your example you will replace this line:
new BufferedReader(new FileReader(new File("src/main/resources/config.txt")));
By this line:
new BufferedReader(new FileReader(new File("classpath:config.txt")));
I have written this project which has a bunch of config files in config/ folder, which are necessary for running:
https://github.com/danyaljj/jwnl-prime/
The code is working fine under mvn test in my computer and CI.
But after packaging it and adding it as a maven dependency to another project, the function calls give me error
java.io.FileNotFoundException: config/file_properties.xml (No such file or directory)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:93)
at net.didion.jwnl.TestDefaults.getInputStream(TestDefaults.java:63)
at net.didion.jwnl.JWNL.initialize(JWNL.java:92)
Any idea why I am getting this weird behavior?
Update:
after extracting the jar file, it turns out that the config files are not included in the jar:
Update2: After adding the suggestion by ? the config files get included in the jar file:
But still getting the same error: config/file_properties.xml (No such file or directory)
If you have project structure other than standard format you need to update pom.xml as well. If you need to include the config for build you need to add that in build section like
Refer link enter link description here
<build>
<extensions>
<extension>
<groupId>org.apache.maven.wagon</groupId>
<artifactId>wagon-ssh</artifactId>
<version>2.4</version>
</extension>
</extensions>
<resources>
<resource>
<directory>config</directory>
<includes>
<include>**/**</include>
</includes>
</resource>
</resources>
</build>
First, verify that the configuration files are actually being packaged into the jar. I would view it in an archive viewer or unpack it and see what's in it.
Secondly, you can't directly load a file from a jar using standard file opening methods. You'll have to use something like
Class.getResourceAsStream()
See here for some more suggestions:
How do I access a config file inside the jar?
I have a maven project which I would like to package in an executable jar.
It's using quite a few dependencies, like spring and so on.
It was suggested in a few posts to use OneJar, to avoid a lot of headaches.
This is what I have currently in my pom.xml:
<plugin>
<groupId>org.dstovall</groupId>
<artifactId>onejar-maven-plugin</artifactId>
<version>1.4.4</version>
<executions>
<execution>
<configuration>
<mainClass>com.cool.project.Application</mainClass>
<onejarVersion>0.97</onejarVersion>
<attachToBuild>true</attachToBuild>
<classifier>coolproject</classifier>
</configuration>
<goals>
<goal>one-jar</goal>
</goals>
</execution>
</executions>
</plugin>
In my Spring configuration one of the classes needs to pass the a resource (src/main/resources/coolfile.bin) path to an external library (jsch) method:
String resource = ConfigurationClass.class.getClassLoader().getResource("coolfile.bin").getFile();
jsch.addIdentity(resource);
When I run Application.java from the IDE (eclipse), the entire application loads successfully.
Although when I run mvn clean install, the onejar jar is built under the target folder, but when I try to run it with java -jar coolproject.one-jar.jar the following error is displayed:
...
Caused by: java.io.FileNotFoundException: file:/target/coolproject.one-jar.jar!/main/coolproject.jar!/coolfile.bin (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:97)
at com.jcraft.jsch.IdentityFile.newInstance(IdentityFile.java:83
If I inspect coolproject.one-jar.jar, I can find the coolproject.jar under the main folder, and if I inspect that, I can see coolfile.bin in its root.
So in theory the resource should be found? What am I missing?
Turns out that FileInputStream would not find the path specified by resource.
Luckily jsch provides another method where you can pass the byte array of the file rather than its location:
jsch.addIdentity("coolfile.bin", toByteArray(ConfigurationClass.class.getResourceAsStream("/coolfile.bin")), null, null);
I have a java project which uses .properties files for configuration. On the server, when launching, I set the classpath to include a folder which contains all the properties files. On my local machine, I'd like to point to a different folder.
I'm looking to add to the classpath, ideally for all projects, but adding it to each project is also fine. I've tried changing the Run > VM Options to include classpath, but with that change it can't find the main class, and I get java.lang.NoClassDefFoundError. I've also tried changing nbactions.xml directly to set the classpath to -classpath ~\MyFolder\;%classpath, but this has the same problem.
To add to the difficulty, the server is running linux while my local machine is running Windows.
I stuck with topic-starter issue also for a long time. My goal - put config files for debug purpose in project root and extend classpath to ${basedir}, so this code:
String appConfigLocation = System.getProperty("config.location");
if (appConfigLocation == null) {
logger.error("System property 'config.location' is not set...");
System.exit(1);
}
InputStream appConfigStream = Main.class.getClassLoader().getResourceAsStream(appConfigLocation);
if (appConfigStream == null) {
logger.error("Can't find resource {} in classpath, fix 'config.location'...", appConfigLocation);
System.exit(1);
}
Properties appProps = new Properties();
try {
appProps.load(appConfigStream);
} catch (IOException ex) {
System.out.println("IO error during loading of {}...", appConfigLocation);
System.exit(1);
}
read configs from ${basedir}. I like that instead of putting them to src/main/resources.
Check sources of ExecMojo.java for v1.2.1 http://grepcode.com/file/repo1.maven.org/maven2/org.codehaus.mojo/exec-maven-plugin/1.2.1/org/codehaus/mojo/exec/ExecMojo.java?av=f :
if ( CLASSPATH_TOKEN.equals( args[i] ) ) {
commandArguments.add( computeClasspathString( null ) );
}
and and v1.3.2 http://grepcode.com/file/repo1.maven.org/maven2/org.codehaus.mojo/exec-maven-plugin/1.3.2/org/codehaus/mojo/exec/ExecMojo.java?av=f:
if ( args[i].contains( CLASSPATH_TOKEN ) ) {
commandArguments.add( args[i].replace( CLASSPATH_TOKEN,
computeClasspathString( null ) ) );
}
So update NB config Execute goals to new version:
process-classes org.codehaus.mojo:exec-maven-plugin:1.3.2:exec
and use complex -classpath args in exec.argsparams:
exec.args=-classpath %classpath:.:"${basedir}" \
-Dconfig.location=app.properties \
-Dlogback.configurationFile=logback.xml \
${packageClassName}
Fix any action you need to have such behavior:
See also:
http://wiki.netbeans.org/FaqEnvVarsDuringRun
http://wiki.netbeans.org/FaqSysPropsDuringRun
Hi I had a similar need to give NetBeans7.4 a classpath to a jar with a driver outwith Maven dependencies e.g. c:\Program Files\Java\jdk1.7.0_25\db\lib\derby.jar in a Java Maven project called MyProject.
As you were thinking with the Run > VM Options, I would suggest the following:
1) right-click on MyProject to open project Properties
2) in 'Project Properties' pop-up, select 'Actions'
3) locate 'Run pproject' among the 'Actions' and select it
4) edit in 'Set Properties' text box entering
exec.args=-cp %classpath;.;"c:\Program Files\Java\jdk1.7.0_25\db\lib\derby.jar" biz.letsweb.derbyconnect.App
exec.executable=java
exec.workingdir=c:\Users\Tomasz\Documents\NetBeansProjects\DerbyConnect\target\classes
Alternatively edit nbactions.xml analogically. Once I did this, I could simply run the project inside NetBeans by pressing green arrow.
what about having the properties included as ? and those you use locally only have in a 's and activate that profile on local machine?
This is how I've been adding classpath to many of my projects
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.nitinsurana.policereports.GUI</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
An outside the box solution I ended up using for my similar case in NetBeans 7.3.1:
Adding files to java classpath at runtime
private static void addSoftwareLibrary(File file) throws Exception {
Method method = URLClassLoader.class.getDeclaredMethod("addURL", new Class[]{URL.class});
method.setAccessible(true);
method.invoke(ClassLoader.getSystemClassLoader(), new Object[]{file.toURI().toURL()});
}
This gives me a hacky way to add files to my classpath at run time via program arguments. Related notes I compiled while researching:
To include a dependency for compilation only, not runtime set:
Dependency Scope
<dependency><scope>provided</scope>...</dependency>
To exclude a dependency from the shaded jar, set: Exclude
<exclude>groupId:artifactId[[:type]:classifier]</exclude>
To copy resources to the target directory from outside the typical source directory: Copy Resources
<project>
...
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.7</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>target/extra-resources</outputDirectory>
<resources>
<resource>
<directory>extra-resources</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
...
</build>
...
</project>
Note that the base path for directory is the project home. The linked post has <filtering>true</filtering> which can cause "invalid mark" in Netbeans 7.3.1.
Hello I'm using a configuration file from src/main/resources in my java application. I'm reading it in my Class like this :
new BufferedReader(new FileReader(new File("src/main/resources/config.txt")));
So now I'm building this with maven using mvn assembly:assembly. Here is the bit for that in my pom.xml :
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.3</version>
<configuration>
<finalName>TestSuite</finalName>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.some.package.Test</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
So when I run my app I get this error :
src\main\resources\config.txt (The system cannot find the path specified)
But when I right click on my assembled jar I can see it inside, anyone knows what I'm doing wrong?
Resources from src/main/resources will be put onto the root of the classpath, so you'll need to get the resource as:
new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/config.txt")));
You can verify by looking at the JAR/WAR file produced by maven as you'll find config.txt in the root of your archive.
FileReader reads from files on the file system.
Perhaps you intended to use something like this to load a file from the class path
// this will look in src/main/resources before building and myjar.jar! after building.
InputStream is = MyClass.class.getClassloader()
.getResourceAsStream("config.txt");
Or you could extract the file from the jar before reading it.
The resources you put in src/main/resources will be copied during the build process to target/classes which can be accessed using:
...this.getClass().getResourceAsStream("/config.txt");
Once after we build the jar will have the resource files under BOOT-INF/classes or target/classes folder, which is in classpath, use the below method and pass the file under the src/main/resources as method call getAbsolutePath("certs/uat_staging_private.ppk"), even we can place this method in Utility class and the calling Thread instance will be taken to load the ClassLoader to get the resource from class path.
public String getAbsolutePath(String fileName) throws IOException {
return Thread.currentThread().getContextClassLoader().getResource(fileName).getFile();
}
we can add the below tag to tag in pom.xml to include these resource files to build target/classes folder
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.ppk</include>
</includes>
</resource>
</resources>
I think assembly plugin puts the file on class path. The location will be different in in the JAR than you see on disk. Unpack the resulting JAR and look where the file is located there.
You can replace the src/main/resources/ directly by classpath:
So for your example you will replace this line:
new BufferedReader(new FileReader(new File("src/main/resources/config.txt")));
By this line:
new BufferedReader(new FileReader(new File("classpath:config.txt")));