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.
Related
getClass().getResource works fine in Eclipse and returns the path for a file I want to have access to, but after exporting the JAR file my files don't load anymore.
I already tried to print out my path; in Eclipse the console shows:
/C:/Users/.../eclipse-workspace/GameJumpAndRun/bin/file/save1
...but if i run the JAR file by using cmd the message is
file/save1, and no files have been loaded.
System.out.println(getClass().getResource("/file/save"+i).getFile());
file = new File(getClass().getResource("/file/save"+i).getFile());
I need the file as a File and not as a BufferedReader; is there a way to get the same file in both Eclipse and the exported 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.
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
I'm trying to read a json file in a Resources directory and I use the following:
jsonObject = this.readJson(this.getClass().getClassLoader().getResource("jsonFileName").getPath());
In the IDE it runs correctly but when I build thw jar and try to run it by java -jar jarName I get a "File not found" Error and when I checked the path, it looks like this:
...projectName/target/projectName-1.0-SNAPSHOT.jar!/kb/is/identity.json
When running on the IDE the paths looks like this:
...projectName/target/classes/kb/is/identity.json
It's important to use getResourceAsStream, not getResource.
Have a look at How getClassLoader().getResourceAsStream() works in java
getResource("jsonFileName") - in this case root directory is project name using it under Idea, but when you run it under jar - I think that root path is User Home.
If I remember correctly, you can fix it with using / in resource path.
E.g. when using Maven, you have a resource directory. You have identity.json in the resource root. Using getClass().getResourceAsStream("/identity.json") receive this file (in Idea and in jar), because when you build a jar, all resources are copied to the root of the jar file.
I really need your help to solve my own problem. Now, I'm dealing with small code app. In that project folder contain some resource files (*.xlsx, *.png,...). I placed them in current folder with code file. I just wonder that when I run my code in netbean ide, it just worked find.
After I build code project, I get a jar file in "dist" directory. I run it. It open normally since app used JFrame as user interface. However, when I execute some function of that app, it showed me the error log. Here is the error message:
java.io.FileNotFoundException:
src\sample.xlsx (The system cannot find the path specified)
What's the matter out there?
Here is some pieces of my code:
copyFile(new File("src\\sample.xlsx"),
new File(txtout.getText()+"\\sample.xlsx"));
Node: copyFile function is used for copy file from source to dest.
Here is my project folder structure in Netbean IDE:
Project Name
Source Pakage(src)
myClass.java, sample.xlsx, etc
First, never reference src directly, the directory will not exist once the program is built. Second, you can not access resources which have been embedded within in the application context via a File reference, they simply no longer exist on the file system.
Instead, you need to use Class#getResource or Class#getResourceAsStream
URL url = getClass().getResource("/sample.xlsx");
InputStream is = getClass().getResourceAsStream("/sample.xlsx");
// Don't forget to manage your streams appropriately...
Well you can create a folder named resources under the src folder put your resources in it and use them in your code by using getResourceAsStream() and getResource() methods that can access the embedded resources.Clean and Build will compile the code and embed the contents of the resources folder into the application’s .jar file.
Ways of Accessing resources :
String pathToImage = "resources/images/filling.png";
InputStream stream= ClassName.class.getResourceAsStream(pathToImage );
String pathToImage = "resources/images/filling.png";
InputStream stream= ClassName.class.getResource(pathToImage );
please refer the link information