简体   繁体   English

如何在不知道确切路径的情况下在 eclipse 项目目录中创建文件?

[英]How to create a file in a eclipse project directory without knowing the exact path?

I have a java class in my Eclipse web-app project that generate a XML file.我的 Eclipse Web 应用程序项目中有一个 java class 生成 Z3501BB093D363ED810B867 文件。 This xml file will be used to display its content in a JSP page through an AJAX call.此 xml 文件将用于通过 AJAX 调用在 JSP 页面中显示其内容。 Suppose I want to export the project and send it to another user who will then use it on another PC.假设我想导出项目并将其发送给另一个用户,然后该用户将在另一台 PC 上使用它。 How can I create the xml file in the project directory even though I don't know the path it will have on the new computer?即使我不知道它在新计算机上的路径,如何在项目目录中创建 xml 文件?

Most Java projects have a standardized project structure.大多数 Java 项目都有标准化的项目结构。 eg Maven looks like follows:例如 Maven 如下所示:

my-app
|-- pom.xml
`-- src
    |-- main
    |   |-- java
    |   |   `-- com
    |   |       `-- mycompany
    |   |            `-- app
    |   |               `-- App.java
    |   `-- resources
    |
    `-- test
        `-- java
            `-- com
                `-- mycompany
                    `-- app
                        `-- AppTest.java

Files a Java application will create with a relative path will read from the resources folder when run locally (of course this can be configures in the runtime environment in Eclipse or tweaked in the java/javac CLI command). Java 应用程序将创建的文件,在本地运行时将从资源文件夹中读取相对路径(当然,这可以在 Eclipse 的运行时环境中配置或在 java/javac CLI 命令中调整)。 Note a packaged Java program cannot write into the packaged JAR or WAR file.请注意,打包的 Java 程序无法写入打包的 JAR 或 WAR 文件。 So when creating a JAR/WAR you can embed static content, but writing doesn't work.因此,在创建 JAR/WAR 时,您可以嵌入 static 内容,但写入不起作用。 So when you run JSP pages classloading comes into play.所以当你运行 JSP 页面类加载开始发挥作用。 Different uses get sandboxed to their own context on the server side.不同的用途在服务器端被沙盒化到它们自己的上下文中。 Therefore sharing the same file and/or disk space might not be a good idea in overall.因此,总体而言,共享相同的文件和/或磁盘空间可能不是一个好主意。

When you have a file DB or similar, you should have a server configuration variable to set the path to the value that's used on your server.当您有文件数据库或类似文件时,您应该有一个服务器配置变量来将路径设置为您的服务器上使用的值。 The complete configuration in your JSP environment is handeled in the web.xml file: https://docs.oracle.com/cd/E13222_01/wls/docs81/webapp/web_xml.html The complete configuration in your JSP environment is handeled in the web.xml file: https://docs.oracle.com/cd/E13222_01/wls/docs81/webapp/web_xml.html

It's the standard way for J2EE containers by the way.顺便说一下,这是 J2EE 容器的标准方式。

If you have a local project and not a fully blown production environment, the simplest options might be reading out an environment variable with the local path you want to use on your machine.如果您有一个本地项目而不是完全成熟的生产环境,最简单的选择可能是读取一个环境变量,其中包含您要在您的机器上使用的本地路径。

Note that if you want to write a file from your J2EE container (=the software that's running the JSP) there might be security rules needed you need to set in Tomcat/Glassfish/Wildfly/...请注意,如果您想从 J2EE 容器(=运行 JSP 的软件)中写入文件,则可能需要在 Tomcat/Glassfish/Wildfly/... 中设置安全规则。

One way is to pass the path as a system property -Dproperty=value (eg java -DxmlDir="C:\xml" SomeClass).一种方法是将路径作为系统属性-Dproperty=value传递(例如 java -DxmlDir="C:\xml" SomeClass)。 On Eclipse, you can add this in Run Configurations, then I think it was either in Arguments or Environment.在 Eclipse 上,您可以在运行配置中添加它,然后我认为它在 Arguments 或环境中。 To access the property, simply do System.getProperty("xmlDir") .要访问该属性,只需执行System.getProperty("xmlDir") On the new computer, they can specify where ever they want the XML file to reside.在新计算机上,他们可以指定希望 XML 文件驻留的位置。

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

相关问题 如何在不知道路径Java的情况下访问项目中的现有目录 - How to access an existing directory in your project without knowing it's path Java 在不知道文件目录或父文件的情况下读取文件路径 - Reading a file path without knowing the file Directory or parent file 如何在Eclipse项目文件中在没有硬编码路径的情况下引用jdk中的库? - How to reference libraries in jdk without hardcoding path in Eclipse project file? 如何在不知道完整文件路径的情况下访问文件 - How to access file without knowing full file path? 如何在不知道确切子字符串的情况下搜索 SpannableString - How to search a SpannableString without knowing the exact substring 如何在不知道Java中的确切名称的目录中获取目录名称 - How to obtain a directory name inside of a directory not knowing the exact name in Java 在不知道绝对路径的情况下访问文件,只知道文件名 - Access file without knowing absolute path, only knowing file name 如何找到文件本地Eclipse项目的路径 - How to find the path of a file local eclipse project 如何为eclipse插件项目创建一个jar文件,该文件应该能够在没有Eclipse软件的情况下下载并运行? - How to create a jar file for the eclipse plugin project that should be able to download and run without eclipse software? 在eclipse中如何创建没有Java项目的类? - In eclipse how to create a class without a java project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM