简体   繁体   English

如何在Java中定义文件路径以使其准备好进入生产阶段?

[英]how to define a file path in java to make it ready for production phase?

I am writing a program in java with netbeans IDE which receives a jasper report *.jrxml and then displays the report for the user. 我正在用netbeans IDE用Java编写程序,该程序会收到jasper报告* .jrxml,然后为用户显示报告。 I wrote the following line of code for the file path 我为文件路径写了以下代码

String reportSource = "src\\jasper-reports\\report.jrxml";  

but when I move the dist folder in some other place and try to run the jar file inside it, my program can not find the report. 但是当我将dist文件夹移到其他位置并尝试在其中运行jar文件时,我的程序找不到该报告。

my problem is that were should I put the *.jrxml file and how to define it's path in my program so that when I want to give my software to someone else it runs without any errors (eg the program can find the file) 我的问题是我应该把* .jrxml文件以及如何在程序中定义它的路径,以便当我想将我的软件提供给其他人时,它可以运行而没有任何错误(例如,程序可以找到该文件)

avoid using absolute paths. 避免使用绝对路径。 try to include the file as a resource in your netbeans project. 尝试将文件作为资源包含在您的netbeans项目中。 then in your code you can search for and load the file as 然后在您的代码中,您可以搜索并加载为

new InputStreamReader((Main.class.getResourceAsStream("/report.jrxml")))

something like that depending on where the file resides in your project 取决于文件在项目中的位置

it's more recommended using one of the two approaches: 建议使用以下两种方法之一:

  1. pass the locations/paths as a -D property=value in the Java application launcher command line http://www.tutorialspoint.com/unix_commands/java.htm 在Java应用程序启动器命令行http://www.tutorialspoint.com/unix_commands/java.htm中将位置/路径作为-D property = value传递

  2. store it the locations/paths in a configurations file with a unique key and edit the file accordingly for different environments, egthis files always stored in ${HOME}/config_files/ directory 使用唯一键将位置/路径存储在配置文件中,并针对不同的环境相应地编辑该文件,例如,此文件始终存储在$ {HOME} / config_files /目录中

absolute paths considered a bad practice 绝对路径被认为是不好的做法

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

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