Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何知道';她在跑步_Java_Jar_Filepath - Fatal编程技术网

Java 如何知道';她在跑步

Java 如何知道';她在跑步,java,jar,filepath,Java,Jar,Filepath,我需要创建一个批处理文件,在windows启动时运行java jar。 为此,我需要知道要自动运行的jar的名称。 我假设用户可以更改jar的名称,因此在创建bat之前,我想检查jar的名称是否已更改 我可以用这段代码得到jar的绝对位置,但是我如何才能得到它的名称呢 File f = new File(System.getProperty("java.class.path")); File dir = f.getAbsoluteFile().getParentFile();

我需要创建一个批处理文件,在windows启动时运行java jar。 为此,我需要知道要自动运行的jar的名称。 我假设用户可以更改jar的名称,因此在创建bat之前,我想检查jar的名称是否已更改

我可以用这段代码得到jar的绝对位置,但是我如何才能得到它的名称呢

    File f = new File(System.getProperty("java.class.path"));
    File dir = f.getAbsoluteFile().getParentFile();
    String path = dir.toString();

谢谢。

方法之一是获取类代码源的URL,例如

    URL r = JdbcTemplate.class.getProtectionDomain().getCodeSource().getLocation();
    System.out.println(r);
印刷品

file:/D:/.repository/org/springframework/spring-jdbc/3.2.4.RELEASE/spring-jdbc-3.2.4.RELEASE.jar

在我的项目中,这没有多大意义。如果用户更改了jar的名称,则无需在批处理文件中“查找”jar。把它指向你已经知道的地方。对于用户将其移动到另一个文件夹的情况,查找这个jar也是一样的。每次jar运行时,都会使用“在windows启动时启动”选项如标记所示,它将使用当前位置和名称重新生成bat文件。如果您检查此处的文档:您将发现系统属性java.class.path可能包含多个您想要的目录。类路径中可以有很多条目,然后您就可以对这些条目进行排序了。Evgeniy Dofofeev的回答是,在我看来,查询类的位置是查找jar文件位置的一种更强大的方法。