JSONTokener crashes after .jar export from eclipse - java

I have a project that writes to a json file, after exporting to .jar and creating the .json externally, it will pause when clicking the button to write.
InputStream is = ClassLoader.getSystemResourceAsStream("Calendar.json");
JSONTokener tokener = new JSONTokener(is); //(Freezes Here)
JSONObject jsonObject = new JSONObject(tokener);
It works perfectly when ran inside eclipse , but after export to .jar it wont get past the JSONTokener

Related

getResourceAsStream() method not working for json file

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'

JAVA :How to Read file in Referenced Libraries

Hi I was reading files using FileReader and Buffereader only and it was working perfectly
fr = new FileReader("C:\\Users\\User\\Documents\\Bus_Station\\Bus station\\tripFile");
br = new BufferedReader(fr);
But I want to read put these files in somewhere so I put them in Referecnced Libraries so I can read from them on any machine without path but it is not working :/
(File file = new File("tripFile.txt");
file.createNewFile();
fr = new FileReader(file););
Any help ?
You should use resources.
URL fileURL = Resources.getResource("test.txt");
String text = Resources.toString(fileURL, Charsets.UTF_8);
How to add a resources folder
How do I add a resources folder to my Java project in Eclipse
adding resources in intellij for java project
How to create resource folder in Netbeans?

Running jar from command line gives FileNotFoundException

This is the code I am running in Java project to read from a text file:
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("wordsEn.txt").getFile());
BufferedReader br = new BufferedReader(new FileReader(file);
When I run this program in Intellij IDEA, everything works fine, but when i build its JAR file, put it in my desktop, and run it through command line from my desktop with the command java -jar SyzygiesGudrat.jar it gives FileNotFoundException
Where do I have to put the text file in order to run this JAR from anywhere or in order someone else to be able to run this JAR when I send it to him?
My project structure looks like this:
Changed the code to the below one, as #MadProgrammer suggested. Now everything works fine.
InputStream in = this.getClass().getResourceAsStream("wordsEn.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(in);
I stucked with the same issue.
Solution is : you can create a jar by IDE or command prompt.
while running a jar in your folder where you store a jar must give txt file.
For example, if you save jar on desktop, then on your desktop must have txt file before you run a jar from command prompt.

jar file not properly finding the files

I have spent the past 3 nights going crazy trying to find an answer to this.
So I have a java program and I want it to be in a jar format and I want it to be able to read in text and image files.
I got the image files working fine using the this.getClass.getResource("") method, however I can not get the program to properly access the text files within the .jar, When I extract the jar, the text files are there so I know It is not a simple mistake of the text files not being within the jar
This is what I tried using, but it didn't work(It works without a jar, but now within a jar)
URL lurl = this.getClass().getResource("list.txt");
BufferedReader in3 = new BufferedReader(new FileReader(lurl.getFile()));
Fixes?
Assuming your class is
my.package.MyClass
The method will read the file from directory /my/package from JAR.
You can open the resource via:
BufferedReader in3 = new BufferedReader(new InputStreamReader(
this.getClass().getResourceAsStream("list.txt")));

Access the folder from jar file

I am trying to use a jar file which itself is a web application in another web project. In my jar which i have created using eclipse's export to jar functionality, I have stored owl files in a folder. To use relative paths in the code in the jar I access it using
MyClass.class.getResource("/folderName").getPath();
and this works fine when I deploy (glassfish) and use the project as a separate application. But when I am using the same from within another project, i changed the path like:
BufferedReader br = new BufferedReader(new InputStreamReader(MyClass.class.getResourceAsStream("/folderName"), "UTF-8"));
ArrayList<File> filesFromCommonOntology = new ArrayList<File>();
while(str = br.readLine() != null)
{
File f = new File(line);
System.out.println(f);
filesFromCommonOntology.add(f);
}
for (int i = 0; i < filesFromCommonOntology.size(); i++)
{
file = filesFromCommonOntology.get(i);
String filePath = file.getPath();
Model timeModel = FileManager.get().loadModel(filePath,
ApplicationConstants.N3);
}
But when i run the project i get null pointer exception for line
BufferedReader br = new BufferedReader(new InputStreamReader(MyClass.class.getResourceAsStream("/folderName"), "UTF-8"));
How to resolve this problem?
You cannot enumerate files in a folder when the application is packaged into jar. It is even difficult to imagine how this could work at all: you open a folder as an inptu stream and read the file names (in that folder) that way?
The simplest way would be to add a text file containing entries in the folder, probably with some descriptions. MyClass.class.getResourceAsStream should open files (not folders) from the jar.
If you have very huge number of files, you can also try to reopen own jar as ZipFile and then you can search for the entries in you folder using ZipFile API. However this is more complex.

Categories