简体   繁体   English

使用Java jar应用程序中的目录

[英]using directories from java jar application

I have java app, where I copy directories (with subdir and files) to SDCard. 我有一个Java应用程序,在其中我将目录(带有子目录和文件)复制到SDCard。 I wanted to save these dirs with project. 我想用项目保存这些目录。 In eclipse everything works fine. 在日食中一切正常。 But, when I export project to runnable jar, app is not able to find or copy these dirs. 但是,当我将项目导出到可运行的jar时,应用程序无法找到或复制这些目录。 No error is showed. 没有显示错误。

File folder2 = new File(getClass().getResource("/urgent/").getFile());
File folder4 = new File(getClass().getResource("/service/").getFile());

The path, I get from jar app, when I want to copy dir is: 当我要复制dir时,从jar应用程序获得的路径是:

jar:file:\C:\path\to\jar\file\myapp.jar!\urgent

project hierarchy: 项目层次结构:

-project
 --src
 --build
 --urgent
  ---subdir
 --service
  ---subdir

Is it possible to work with your own directories in jar and how? 是否可以在jar中使用您自己的目录?如何使用?

You are trying to work on resources on you classpath, a random folder on your harddrive is not in it, so you cannot find it. 您正在尝试使用类路径上的资源,但硬盘驱动器上没有随机文件夹,因此无法找到它。

What you need to do is: 1. Work with parameters from the commandline (see how to use args[] in your main method) 2. use those arguments to parametrize your custom copy mehod 您需要做的是:1.在命令行中使用参数(请参阅如何在主方法中使用args [])2.使用这些参数来参数化您的自定义复制方法

or: 要么:

Try using the copyDirectory(File srcDir, File destDir) method from the Apache Commons IO library instead. 尝试改用Apache Commons IO库中的copyDirectory(File srcDir,File destDir)方法。 To use this reearch how to include libraries in your project, preferably by using maven. 若要使用此方法,最好通过使用Maven,在项目中包括库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM