我正在使用aQute Bnd工具集来创建一个OSGi包,并打包了一些相关的"资源"文件.这包括我创建的资源目录中的*.css文件和*.xsd文件.
我在bundle.bnd文件中包含以下内容:
Include-Resource: resources/=resources/
Run Code Online (Sandbox Code Playgroud)
当我进行构建时,生成的*.jar文件在jar包文件的顶级目录的resources目录中包含*.css和*.xsd文件.
但是,在实际代码中,我很难尝试将其作为类路径的一部分引用:
我尝试过以下方法:
new File("resources/example.css");
Run Code Online (Sandbox Code Playgroud)
我也尝试过:
URL cssFile = this.getClass().getResource("resources/example.css");
try
{
file = new File(cssFile.toURI()));
}
catch(Exception e)
{
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
我得到一个NullPointException错误或无法找到文件IOException错误(取决于我使用哪一个).我在调试配置模式下运行Eclipse Equinox以及Apache Felix(我们用于部署)时遇到此错误.注意我试图在BundleActivator之外的Java类中执行此操作.
我是否需要始终参考BundleActivator的上下文,例如?
/*
* (non-Javadoc)
* @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
*/
@Override
public void start(BundleContext context) throws Exception
{
/* Service to host the Bundle interface. */
ServletContainerService service = new ServletContainerService();
service.addServlet(new ServletContainer(new AxisServlet(), true));
this.serverReg = context.registerService(ServletContainerService.class.getName(), service, null);
cssFile = new File(context.getClass.getResource("resource/example.css"));
}
Run Code Online (Sandbox Code Playgroud)
我认为上面的内容会起作用,但意味着我将不得不传递cssFile参考,而这个参考似乎并不优雅.
有没有办法在bundle/.jar文件的任何给定Java类中引用bundle jar文件中包含的'resources'目录的路径?如果它涉及BundleContext,有没有办法在任何Java类中引用它?
任何帮助都感激不尽.
我已经看过并 …
我已经下载了ANTLR Java运行时库.这是一个jar文件.它包含"org.antlr.runtime".我在哪里放置它以便Java编译器可以找到它?我需要提取它吗?我正在使用NetBeans(现在),如果相关的话.
我目前有一个Java测试库,它是用Maven构建的,并以jar形式发布.我的项目依赖于一个非常常见的库(Objectweb ASM),并且我遇到过类似路径中已经存在早期且不兼容的ASM版本的问题.因此,我开始使用jarjar-maven-plugin来创建jar,在内部重新打包ASM,它不会与另一个版本的ASM冲突.
这执行得很好,我的库可以作为依赖项拉入,没有问题.
但是,因为我的项目在ASM上具有编译范围依赖性,所以每当客户端项目添加我的库时,传递依赖关系也都被拉入.因此,假设,如果他们使用特定版本的ASM,并且他们还将我依赖的版本添加到类路径中,则它们具有未定义的行为.我想避免这种情况,并允许客户端依赖JarJar'd工件,而不会让Maven不必要地和可能危险地降低传递依赖性.
如何创建一个JarJar'd工件,用户可以依赖它而不会提取传递依赖关系?
如果我有两个同名的类在我的类路径中的两个不同的jar中说Matcher.java,其中一个将由JVM拾取,那么我是否可以建议JVM选择一个特定的?
我要做的是编写一些Maven插件,它扫描应用程序类,寻找特定接口的实现(它可能是带有一些注释的类),并根据结果生成一些代码.我已经成功实现了在generate-sources阶段运行的插件,并将源代码编写到generated-sources目录.
问题在于使用某些注释扫描具有特定接口实现/类的类路径.我使用Reflections库以下列方式扫描类:
private Set<Class< ? extends MyInterface >> scan(final String packageName) {
final Reflections reflections = new Reflections(packageName);
return reflections.getSubTypesOf(MyInterface.class);
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,此方法返回空集.当我在类扩展中打印我的类路径时org.apache.maven.plugin.AbstractMojo(我正在使用Reflection),我得到以下结果:
/home/pd5108/apache-maven-2.2.1/boot/classworlds-1.1.jar
我希望使用Reflections找到的类存在于dependend JAR以及配置插件的模块中.看看打印出来的类路径,似乎此时(生成源阶段)maven中定义的依赖关系在类路径上都不可用 - 可能它们会在下一阶段添加.真的吗?我还可以使用其他方法吗?
以下是打印类路径的方法:
URL[] urls = ((URLClassLoader)sysClassLoader).getURLs();
for(int i=0; i< urls.length; i++) {
System.out.println(urls[i].getFile());
}
Run Code Online (Sandbox Code Playgroud) 我想从我的类路径中获取一个xml文件来解组它并将其用于测试目的.我的问题是将它作为InputStream.我写了这些行,但我总是得到一个null结果.
InputStream is = getClass().getResourceAsStream("WebContent/WEB-INF/classes/testing/"+ COMPLETE_DOCUMENT + ".xml");
Run Code Online (Sandbox Code Playgroud)
当然,你在方法参数中看到的路径是我文件中的路径.我尝试了几种组合:
WebContent/WEB-INF/classes/testing/
classpath:testing/
classpath*:testing/
Run Code Online (Sandbox Code Playgroud)
但我总是得到InputStream = null.
我甚至试图切换到
ClassLoader.getResourceAsStream(...)
Run Code Online (Sandbox Code Playgroud)
但没有任何反应.我想资源的路径是错误的,但我无法弄清楚在哪里.从我的servlet.xml中,我使用类路径中的一些资源来配置PropertyPlaceholderConfigurer或Jaxb2Marshaller,只需使用语法
"classpath:folder/file.xsd"
Run Code Online (Sandbox Code Playgroud)
它完美无缺.我想要加载我的xml的文件夹是上面示例中的一个兄弟.我错过了什么?
编辑: 我尝试遵循Spring ClassPathResource帮助程序类方法,我得到一个奇怪的行为:正如我之前所说,我已经有一些资源从类路径加载启动时的一些spring bean.如果我在dardo建议的代码中使用这些资源的路径如下:
ClassPathResource cpr = new ClassPathResource("xmlschemas/lrinode.xsd");
InputStream is = cpr.getInputStream();
Run Code Online (Sandbox Code Playgroud)
我还是得到了FileNotFound Exception!当然"xmlschemas/lrinode.xsd"是我成功启动时加载的xsd.即使我从应用程序的根目录开始使用资源的完整路径,它也不起作用.
我开始认为我错过了一些微不足道的东西.
我有一个Java EE项目.该项目使用maven构建为.ear存档.有一个包含JPA 2持久性单元的库jar,它位于耳朵的库目录中(因此多个其他模块可以使用它).
虽然在这个持久性单元中添加了Shiro的Permission接口的实现作为实体,但我无法正确部署,因为shiro类在持久性单元中不可用.我最终发现我需要在库目录中放置库jar的所有依赖项(也适用于传递deps)以使其部署.
所以最终的布局看起来大致如下:
ear
`- lib
`- persistence-unit.jar
- shiro-core.jar
- slf4j-api.jar
- module1
- moduleN
- library1.jar
- libraryN.jar
Run Code Online (Sandbox Code Playgroud)
现在,对于问题:
编辑:耳朵的pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>ear</artifactId>
<packaging>ear</packaging>
<parent>
<groupId>com.example</groupId>
<artifactId>project</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ear-plugin</artifactId>
<version>2.7</version>
<configuration>
<modules>
<webModule>
<!-- ... -->
</webModule>
<ejbModule>
<!-- ... -->
</ejbModule>
<jarModule>
<groupId>com.example</groupId>
<artifactId>persistence-unit</artifactId>
<bundleDir>lib</bundleDir>
</jarModule>
<-- I added these to get the deployment working -->
<jarModule>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<bundleDir>lib</bundleDir> …Run Code Online (Sandbox Code Playgroud) 我有一行代码:File file = new File(getFile())在java类中HandleData.java
方法 - getFile()获取属性的值fileName.并fileName通过application_context.xml类的bean部分注入 - HandleData如下:
<bean id="dataHandler" class="com.profile.transaction.HandleData">
<property name="fileName" value="DataFile.xml"></property>
</bean>
Run Code Online (Sandbox Code Playgroud)
我成功地构建了项目并检查了 - DataFile.xml是否存在WEB-INF/classes.并且HandleData.class存在于WEB-INF/classes/com/profile/transacon
但是当我运行它时会抛出filenotfound异常.如果我注入绝对路径(C:\MyProjectWorkspace\DataProject\target\ProfileService\WEB-INF\classes\DataFile.xml它成功找到文件).
有人可以帮助确定要注入的正确路径,以便从类路径中获取文件吗?
我正在创建控制台应用程序 我想在jar文件conf夹中的文件外部配置文件,并希望将此文件夹注册为我的应用程序的类路径.
我运行mvn assembly:single命令,得到一个jar文件,但是当我尝试运行这个JAR时java -jar MyApplication.jar,它无法读取配置文件.
我有这个片段 pom.xml
<build>
<finalName>MyApplication</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.7</version>
<configuration>
<projectNameTemplate>
[artifactId]-[version]
</projectNameTemplate>
<wtpmanifest>true</wtpmanifest>
<wtpapplicationxml>true</wtpapplicationxml>
<wtpversion>2.0</wtpversion>
<manifest>
${basedir}/src/main/resources/META-INF/MANIFEST.MF
</manifest>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<appendAssemblyId>false</appendAssemblyId>
<archive>
<manifest>
<mainClass>com.my.test.App</mainClass>
</manifest>
<manifestEntries>
<Class-Path>.conf/</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>
Run Code Online (Sandbox Code Playgroud) 好的我们在带有外部Sqlite驱动程序库的unix主目录中有一个Java应用程序"app.jar".
- myapp/app.jar
- myapp/lib/sqlite-jdbc-3.8.7.jar
Run Code Online (Sandbox Code Playgroud)
设备Udoo ARM Cortex V9与Raspberry Pi相似.
java -version
java version "1.8.0_06
Java(TM) SE Runtime Environment (build 1.8.0_06-b23)
Java HotSpot(TM) Client VM (build 25.6-b23, mixed mode)
Run Code Online (Sandbox Code Playgroud)
尝试运行此应用程序失败.
java -classpath lib/sqlite-jdbc-3.8.7.jar -jar myapp.jar
Run Code Online (Sandbox Code Playgroud)
似乎应用程序找不到库.
java.lang.Exception: No native library is found for os.name=Linux and os.arch=arm
at org.sqlite.SQLiteJDBCLoader.loadSQLiteNativeLibrary(SQLiteJDBCLoader.java:284)
at org.sqlite.SQLiteJDBCLoader.initialize(SQLiteJDBCLoader.java:65)
Run Code Online (Sandbox Code Playgroud)
直到现在我都找不到解决方案.可以将.jar文件复制到jdk/lib文件夹吗?
编辑:
现在我尝试将sqlite-jdbc-3.7.2.jar添加到JRE中并且它可以正常工作.
jdk1.8.0_06/jre/lib/ext
Run Code Online (Sandbox Code Playgroud)
较新版本sqlite-jdbc-3.8.7.jar不起作用.