getResourceAsStream() method not working for json file - java

I a trying to create a .jar file of my JavaFX project in eclipse. I have moved all resources (images and a JSON file) into the res folder and configured it as a source folder. I used
Image iFloorPlan = new Image(this.getClass().getResourceAsStream("/Floor-Plan.jpg"));
for my Image, and it works perfectly, but when I try to do File f = new File(this.getClass().getResourceAsStream("Murder-on-the-2nd-Floor-Raw-Data.json"));
, eclipse underlines it and says 'The constructor File(InputStream) is undefined'

Related

Resources not loading after building the app to jar

My app works perfectly in the IDE, but when I build it to a jar the resources are not loaded at all. I tried using
this.getClass().getClassLoader().getResourceAsStream("path")
that didn't work as well.
my project structure looks like this:
I'm using Eclipse IDE,and I want to load the following:
1 - shared MP3 file (current.mp3)
2 - icon for swing (icon.jpg)
these 2 files located in resources folder.
i added resources as a source folder.
Image icon = Toolkit.getDefaultToolkit().getImage("resources/icon.jpg");
player = new Player(new FileInputStream("resources/current.mp3"));
OutputStream outstream = new FileOutputStream(new File("resources/current.mp3"));

Unable to access the resources file in jar after packaging : FileSystemNotFoundException

Hi I am working on developing a small java application (kinda new to this ) which takes an input file and gives out an web page. Here the HTML template and an image file to the web page are in resource folder.
Below is the way I am accessing the html template file in my code.(I need to read the HTML template as an string)
{
String htmlString;
ClassLoader classLoader = getClass().getClassLoader();
URL fileurl = classLoader.getResource("template.html");
htmlString = String.join("\n", Files.readAllLines(Paths.get(fileurl.toURI())));
}
the code works fine when ran from eclipse but fails when I run it as an jar.(java.nio.file.NoSuchFileException:)
kindly help me in accessing the template file from resources folder and also note that the html takes an image from the same folder. Now the template and image file must be packaged and should be able to run as a single JAR .
update :
Extracted the jar file with 7-zip to see the contents. I can see the files in resources are available with the jar
Thanks!
You have packaged everything into a jar so the pathnames to resources is no longer a valid path to a file. If you added this logging you would see that the path to the jar entry says something like jar:file:/C:/path/to/your.jar!/template.html which cannot be opened by Files.readAllLines:
URI uri = fileurl.toURI();
System.out.println("uri="+uri);
The solution is easy, just locate the resource as InputStream which must be on the classpath:
String htmlString;
ClassLoader classLoader = getClass().getClassLoader();
try(InputStream in = classLoader.getResourceAsStream("template.html")) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
in.transferTo(out);
htmlString = new String(out.toByteArray()); // as platform encoding
}
This works if the res directory is part of your classpath when you run the code in exploded directory, and works in JAR file if the contents of "res" are inside the jar.

I want to upload file (img/ppt/apk) from the project folder but not by giving xpath of my system file path

I want to upload the file which can be in format of Png/Jpg/Apk/PPT/Pdf/Word to my web Service Project. Currently I am using Selenium WebDriver and Maven project with TestNG framework.
Issue Facing - I want to upload all files directly from my folder which is inside the project but outside SRS in Eclipse. I am getting an error of Null pointer exception or some time file not found error.
Below is the code which I am using to upload
AddNewAppPage lPO = new AddNewAppPage(driver);
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("./Drivers/ABC.pdf").getFile());
Thread.sleep(3000);
lPO.getPresentationFile().sendKeys(file.toString());
Actually I don't want to give XPath of image or file upload of my system, I directly want to upload from the resource folder.

File only accessible when placed in a location where it will be erased from the jar

I'm new to NetBeans IDE, and am struggling with accessing a file after building the jar file. After reading through many posts on this topic, I decided to try the following code:
BufferedReader read = new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("/file.txt")));
This works fine when my file is placed inside the "build" folder of the project where the .class files are, but of course this is a problem because it is erased in the "clean and build" process when the jar file is created. I have tried placing it in the src folder, in a separate "resources" package, and in the root of directory. I have also tried calling getResourceAsStream() with "file.txt" and "/src/file.txt," but it only works in the above configuration when the file is with the .class files. Any tips would be much appreciated!
Why not have your file folder inside the tomcat bin and refer the directory from your code. So maven clean will not alter the files and you can remove, update file without needing to restart the application. ( here i have file inside etc )
Path: /Users/username/Documents/apache-tomcat-8.5.15/bin/etc
ArrayList<String> readList = null;
String workingDir = System.getProperty("user.dir");
String fileName = "File.txt";
File file = new File(workingDir+"/etc/" + fileName);
readList = resourceReader.readFile(file.getAbsolutePath());
I have method readFile to parse some data and build the ArrayList in the above example.
Read about System Properties
Turns out the solution was really simple...I had been trying to manually create a resources folder, but the contents kept being deleted upon building of the jar. Instead, I created a resources package and put the file into the auto-generated folder inside the src folder, which packaged the file into the jar. Thanks everyone!

putting picture in java program using MyEclipse GUI

I want to add a picture to my GUI program created using Eclipse and MyEclipse (for GUI visual design) from the resource pictures I pasted earlier in the project.
I managed to load pictures that lies just beside the .JAR file using
image = ImageIO.read(new File("imageFile.jpg"));
But I want to use the image from my resources "src" folder directly , so that the .JAR file is a standalone file yet loads pictures nicely.
I tried to make it
image = ImageIO.read(new File("src/ldtlogo3.jpg"));
I use this method when exporting the .JAR file
Java: export to an .jar file in eclipse
Use the overloaded ImageIO.read method taking an InputStream as a parameter, and use MyClass.class.getResourceAsStream() to get this input stream. getResourceAsStream loads a resource from the classpath (and thus from the JAR of your application). Its api doc will tell you which path it expects.
Note that the src directory is used to hold your Java source files. The jar doesn't contain it. It contains the .class files, in a hierarchy which directly maps the package hierarchy. Eclipse will automatically "compile" the image file by copying to the output directory, along with the .class files.

Categories