I have a gradle project which is working fine within eclipse. In the java code I have
Properties p = MyClass.class.getClassLoader.getResourceAsStream("myProps.properties");
File f = new File(p.getProperty("myFile"));
My structure is:
src/main/java - has java files
src/main/resources - has myProps.properties and myFile.txt
myProps.properties has:
myFile=src/main/resources/myFile.txt
If I do a gradle build (using a default shadowJar task) my resources have now been moved from src/main/resources to the root of the jar file.
shadowJar {
manifest{
// The only thing I modify is the attributes
}
}
When I execute the jar by running
"java -jar myJar.jar" I get FileNotFound exception referring to the myFile.txt resource file.
What is the correct way to fix this issue so that the code works from both the Eclipse and executable jar environments?
I needed to read the file as a stream (the same way I read in the properties file). I removed 'src/main/resources' from the path in the properties file and it worked
Related
I have a swing project structure in eclipse like the following
MyProject
--src //the java source files
--CustomFolder
-abc.txt
-def.bat
I am accessing abc.txt and def.bat using
File theFile = new File("CustomFolder\abc.txt");
When I run this in eclipse its working fine. But when I export JAR of the project and execute using JAR then it is throwing exception of FileNotFound. I tried using following as well from this link
URL fileUrl = getClass().getClassLoader().getResource("CustomFolder\abc.txt");
but the fileUrl is coming as NULL. So my question is how to provide a file path so that it could be accessed when created a JAR or after creating an exe of the JAR.
I am creating the client-server application in java. I have created three modules:
Client
NetworkModels - serializable models
Server
Client should ask the server to parse the xml file, xml file should be stored on the server. I have setup two build configurations as on the screen, one for the client and one for the server:
I have added file "Employees.xml" like on the next screen, I was expected that file will copied to output directory on the build
But actually when run build, the file not copied to the ouput, all I have is jar:
I want this to be able to place a xml file in the same directory as server.jar and than run the server.jar, see this code, which tries to read the file from the same location where executing jat is placed:
private File getEmployeesXmlFile() throws Exception {
var classLoader = ClassLoader.getSystemClassLoader();
var pathToExecutedJar = Main.class.getProtectionDomain().getCodeSource().getLocation().getPath();
var tempFile = new File(pathToExecutedJar);
var currentDirectory = tempFile.getParentFile().getAbsolutePath();
var pathToEmployeesXml = currentDirectory + "/employees.xml";
var employeesXmlFile = new File(pathToEmployeesXml);
if (employeesXmlFile == null) {
throw new Exception("Employees xml file not found at " + currentDirectory);
}
return employeesXmlFile;
}
The xml file is undo server module on resources/employees.xml path, I have added this wildcards to resource patterns, hoping that file will be copied to the server output, but it not helps:
What I want is to edit file in IDE and copy it to the output on build. Currently I have placed 'employees.xml' manually in the out dir.
I want this to be able to place a xml file in the same directory as server.jar
You say you want to place it near the jar but you include it into a jar artifact - so it is packed inside the jar.
For IDE to copy the resource file to output dir: the file must be placed in the resources directory. Then see the module's output directory where IDE places this file on build.
What you can do to place it near the jar is to configure an Ant task in Post-processing tab in the artifact configuration to be run after IDE builds the artifact.
After reading about 50 different threads on the subject, I have not been able to have any of the solutions work.
What I'm trying to do is to access my config.properties file within the jar.
My project is a Maven project and the architecture is as follows:
My project
src/main/java
myApp
MyClass.java
src/main/resources
icon.jpg
config.properties
So when I try to access it from Eclipse it works perfectly good with this code:
InputStream configFile = MyClass.class.getResourceAsStream("config.properties");
Followed by more code here to retrieve properties...(that works)
However, when I export the project to a runnable JAR, it doesn't work any more. Inside the jar, files are as follows:
File.jar
MyApp
files.class here
etc...
META-INF
resources
icon.jpg
config.properties
Try this:
InputStream configFile = MyClass.class.getResourceAsStream("resources/config.properties");
Try the following:
InputStream input = AnyClass.class.getResourceAsStream("/config.properties");
It should work.
My maven project is a standalone java appplication. I need to run this form a Unix box. So i made a runnable jar.But i have to update a date in the app.properties file in every run.I tried maven jar plugin to make a fatty runnable jar with all dependency. It is running fine , but not able to edit the app.properties file
You can read a properties file from the classpath (root package) like this:
Properties properties = new Properties();
properties.load(this.getClass().getResourceAsStream("/app.properties")));
You can start a runnable jar with a custom classpath like this:
java -cp app.properties:app.jar my.main.AppClass
(you cannot use java -jar because then the -cp option is ignored)
Put the file app.properties in the same directory as app.jar. The location of app.properties will be the first entry on the classpath and thus the code in the jar can load it as shown above.
If you make sure there is also a copy of app.properties in the jar, then that will be used as default if the external properties file is missing. (note that this only works for the complete properties file, not for individual properties)
I currently have two projects:
api-test
...
/config/config.json
...
and
ui-test
...
/config/config.json
...
In eclipse, I am adding api-test in the build path of ui-test, so that api-test is the dependency of ui-test.
However the build failed, because api-test is looking for the config.json located in api-test/config/config.json by calling:
System.getProperty("user.dir") + "/config/config.json"
which does not exist in ui-test project.
the two config.json include different contents - what would be the best solution to let each project refer to their own config.json while ui-test is referring to api-test project?
Put the files into the projects' src/main/resources directories as suggested by Maven's Standard Directory Layout. You can use relative paths to access these resources then.
See How to get file resource from Maven src/test/resources/ folder in JUnit test? For instance:
Test file existence
#Test
public void testStreamToString() {
assertNotNull("Test file missing", getClass().getResource("/sample.txt"));
...
}