使用classpath参数运行jar [英] Run jar with classpath parameters

查看:135
本文介绍了使用classpath参数运行jar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我可能错过了一些愚蠢的东西但是仍然。

我创建了一个具有以下内部结构的jar文件:


I'm probably missing something stupid but still.
I created a jar file with the following inner structure:


  • folder1

  • folder2

  • META-INF

  • 资源

  • folder1
  • folder2
  • META-INF
  • resources

其中folder1和folder2保存.class文件和资源有txt和png文件。

在我的代码我有以下行

where folder1 and folder2 hold .class files and resources has txt and png files.
In my code i have the following line

BufferedReader reader = new BufferedReader(new InputStreamReader(
                ClassLoader.getSystemResourceAsStream(txtFile)));

其中 txtFile 是一个包含文件的字符串位于资源文件夹中的名称。 (直接在文件夹中)。

问题是当我尝试从命令行运行程序时。

这是我运行的命令。

where txtFile is a string holding a file name that is located within the resource folder. (directly in the folder).
The problem is when i try to run the program from the command line.
This is the command i run.

java -cp externalJar.jar;myJar.jar;resources com.my.package.MyMainClass

程序运行和精细单元我到达上面的代码行,我得到一个空指针异常:

The program runs and fine unit i get to the above code line where i get a null pointer exception:

java.lang.NullPointerException
at java.io.Reader.<init>(Unknown Source)
at java.io.InputStreamReader.<init>(Unknown Source)
at com.my.package.MyMainClass.generateSomething(Unknown Source)
at com.my.package.MyMainClass.main

我想这意味着我的类路径有问题 - 但我无法想象...

which i guess means that there is something wrong with my classpath - but i can't figure what...

编辑

我正在使用ant编译程序并将程序打包到一个可执行jar。

这是我的一部分 build.xml

<target name="jar" depends="compile">
    <jar destfile="${executable.jar}" basedir="${bin}">
        <zipfileset dir="${src}\resources" prefix="resources" />
        <manifest>
            <attribute name="Main-Class" value="MyMainClass" />
            <attribute name="Class-Path" value="externalJar.jar" />
        </manifest>
    </jar>
</target>

编辑 - 2

因为它应该是要成为可执行jar, txtFile 变量的值是硬编码的(除非作为参数给出)作为资源目录中的文件名。所以最终,我想运行这个命令:
$
java -jar myJar.jar

不幸的是 - 这也是结果相同(同一行上的NullPointerException)

编辑 - 3(找到非优雅解决方案)

我发现如果我连接到类加载器读取的文件的所有路径前缀resources /然后它工作正常 - 但我发现这是不优雅的。有没有办法将资源文件夹添加到类路径?

EDIT - 2
Because it's supposed to be an executable jar the value of the txtFile variable is hard coded (unless given as an argument) to be the file name inside the resources directory. So eventually, i would like to run this command:
java -jar myJar.jar
Unfortunately - this also results in the same manner (NullPointerException on the same line)
EDIT - 3 (FOUND UN-ELEGANT SOLUTION)
I found that if i concatenate to all the paths of files that are read by a class loader the prefix "resources/" then it works fine - but i find this to be un-elegant. Is there no way to add the resources folder to the classpath?

推荐答案

首先,如果您只打算访问其中的文件与 MyMainclass 相同的jar,你可以调用 getResource() getResourceAsStream()在其类加载器而不是 getSystemResource() / getSystemResourceAsStream()

First, if you only plan to access files that are within the same jar as your MyMainclass, you can call getResource() or getResourceAsStream() on its class loader rather than getSystemResource() / getSystemResourceAsStream().

public class MyMainClass {

    public static void main(String[] args) throws Exception {
        InputStream inputStream = MyMainClass.class.getResourceAsStream(args[0]);
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
    }
}

其次,你应该把路径传递给文件而不是而不是在类路径上传递resources文件夹:

Second, you should pass the path to the file rather than passing the resources folder on the classpath:

java -cp externalJar.jar;myJar.jar com.my.package.MyMainClass "/resources/myTextFileInUtf8.txt"

请注意,它(几乎)总是一个好主意在创建InputStreamReader时指定编码,否则您依赖于JVM默认编码。

Please also note that it is (almost) always a good idea to specify the encoding when creating an InputStreamReader, otherwise you rely on the JVM default encoding.

这篇关于使用classpath参数运行jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆