简体   繁体   English

给Java项目中的文件提供固定路径(Netbeans)

[英]give fixed path to file in Java project (Netbeans)

I have a Java project generating jasper reports, what I do is I give path of a file (report) and run my project it runs fine, but when I have to give JAR file to my friend, it always need that file to be placed in that specific folder according to which my code get's the file path and then have to place file there, my code is below to give some idea 我有一个Java项目生成jasper报告,我做的是我给出一个文件的路径(报告)并运行我的项目它运行正常,但是当我必须将JAR文件提供给我的朋友时,它总是需要放置该文件在那个特定的文件夹中,我的代码得到的是文件路径,然后必须将文件放在那里,我的代码在下面给出一些想法

public void generateReport(String dateStart, String dateEnd) {
    InputStream stream = null;
    Connection connection = null;
    Class.forName("com.mysql.jdbc.Driver");
    connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/app", "root", "");
    stream = new FileInputStream(new File("").getAbsolutePath() + "/reports/logReport.jrxml");
    JasperDesign jasperDesign = JRXmlLoader.load(stream);
    jasperReport = JasperCompileManager.compileReport(jasperDesign);
    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, connection);

    //frame.getContentPane().add(dateSetter, BorderLayout.PAGE_START);
    frame.getContentPane().add(new JRViewer300(jasperPrint), BorderLayout.CENTER);
    frame.setResizable(false);
    frame.setSize(900, 600);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    isVisible = true;
    connection.close();
    stream.close();
    }

within code it get the file from the path as given 在代码中,它从给定的路径获取文件

/reports/logReport.jrxml /reports/logReport.jrxml

but when I put JAR file somewhere else and run it, it gives error that 但是当我将JAR文件放在其他地方并运行它时,它会给出错误

C:\\Desktop\\reports\\logReport.jrxml cannot find path specified. C:\\ Desktop \\ reports \\ logReport.jrxml找不到指定的路径。

I know it's due to code part new File("").getAbsolutePath() but I need to know how to make it so that it always get the file path from within project! 我知道这是由于代码部分new File("").getAbsolutePath()但我需要知道如何使它始终从项目内获取文件路径! So that I don't always have to place that particular file there! 所以我并不总是把那个特定的文件放在那里!

If you want to make it an absolute path to the same directory always you just specify the entire path. 如果您想使它成为同一目录的绝对路径,您只需指定整个路径。

stream = new FileInputStream(new File("C:/path/to/my/reports/logReport.jrxml"));

If you want it relative to the directory where your jar is running. 如果你想要它相对于jar运行的目录。

stream = new FileInputStream(new File("path/to/my/reports/logReport.jrxml"));

If instead you are looking to load the file from your Jar itself, you need to do it differently. 如果您希望从Jar本身加载文件,则需要采用不同的方式。

stream = this.getClass().getResourceAsStream.("/path/to/my/reports/logReport.jrxml");

Better you read the file form a specified location 最好从指定位置读取文件

Here is the link to read the file from a specified location. 以下是从指定位置读取文件的链接

but if the same code is running on a server you have to specify the path relative to the root directory where the server is running 但是如果在服务器上运行相同的代码,则必须指定相对于运行服务器的根目录的路径

 String location = "/opt/reports/";
 String filename = "logReport.jrxml";

 File file = new File(location + filename);

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

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