Got a little problem loading a file! (Not a image just really a file like .txt and stuff)
It loads fine in Netbeans with
File myfile = new File("a/b/myfile.abc");
The problem is the compiled jar gets a exception and doesn't find the file. I need it as a file, not as a Stream or something, that's the problem and I have already tried everything that came into my mind to load it.
I would like to load it externally (not from inside the jar) and the problem is it doesn't seem possible to get a working setup with getRessource(AsStream).
EDIT:
Ok so i let it print the absolute path when it was compiled and when it was not compiled.
Non compiled path:
C:\Users\USERNAME\Documents\NetBeansProjects\ProjectName\a\b\myfile.abc <-- Correct Path
Compiled path:
C:\Users\USERNAME\a\b\myfile.abc <-- Not Correct Path
Can anybody tell me how to fix this?
EDIT²:
If I navigate to the correct folder with cmd (cd etc.) and start the jar after doing so the folder is getting loaded from the correct directory. Can someone tell me what I need to change?
EDIT³:
When not starting with cmd it seems to search for the folder in Windows/system32 :O
When you construct a file with
new File("abc.txt")
the abc.txt file is supposed to be in the current directory. The current directory is the directory from which the java command is launched to execute your application. So, if you're in c:\foo\bar and execute java -cp d:\java\app\MyApplication.jar MyApplication, it will look for the file c:\foo\bar.
The location of the jar of the application is irrelevant, and doesn't have any impat on where the file is looked up. The current directory is what matters.
Related
I’m working on a background Spring service that runs from the command line using a nohup command.
I’m hitting the following error:
java.io.FileNotFoundException: class path resource [templates/] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/Users/gestes/Documents/workspace/bge/bge-proj/myProcess/target/myProcess-0.2.2.jar!/templates/
The jar file is being created and does exist at:
/Users/gestes/Documents/workspace/bge/bge-proj/myProcess/target/myProcess-0.2.2.jar
When I extract the jar file contents, there is a /templates/ directory.
Looking at the exception, there is an “ ! “ after the jar file name, and I thought that tells what it can’t find, but clearly, it is there.
What am I missing?
If in your code you are trying to access the folder using a java File, you cannot do that. You need to use an inputStream
This is because things inside a Jar are not actually files on the disk. They are compiled inside of a jar. Yes it may be there when you extract the jar, but it's not actually a normal disk file when the jar is bundled
The code in question is simple something along the lines of
File f = new File("Testfile.txt");
f.createFile();
the file would later be written into. This code is in an executable jar file. When it is executet via "java -jar jarname.jar" it works fine, but when it's being launched via normal doubleclick, it does not work. Nothing happens. So far this seems to be the case because the file created somehow does not actualy denote a file in the directory the jar file is in but that is just an assumption. For short: how can I create and edit a file from a runnable jar that is being run via doubleclick?
Thanks in advance
So far this seems to be the case because the file created somehow does not actualy denote a file in the directory the jar file is in but that is just an assumption.
Correct. It doesn't. It represents a file in the current working directory. What that is when you double-click depends entirely on how the double-click is set up. Possibly the root directory.
For short: how can I create and edit a file from a runnable jar that is being run via doubleclick?
Why? Why create the file before it's written into? Don't do this. The time to create the file is when you create your file output stream or writer. If that fails you get an exception too, which is more helpful than the Boolean returned by File.createFile(), which in any case you are ignoring.
This issue is of different nature: The files DO get created - the problem is that they're created in the executing users home directory where you can easily miss them (and don't want them). The solution is to get the current Locatin of the executed jar file as discussed here: How to get the path of a running JAR file?
I hope this helps,
Cheers!
I'm building a Groovy plugin for Android Studio which will execute some gradle tasks. However I need to load a XML file from within the JAR file from where the code executes.
The JAR file is as following:
com/packagename/code
META-INF/gradle-plugins
filename.xml
Because the xml file is at the root of the JAR I used this to load in the resource into an inputstream
new InputStreamReader(CheckStyleTask.class.getResourceAsStream("/filename.xml"))
The weird thing is that it only works sometimes. Sometimes it returns the file, sometimes it returns null. For example I took this line to check the path:
println(CheckStyleTask.class.getResource("/filename.xml").getPath())
And the path is completely right. So it is able to find the file with getResource, but with getResourceAsStream it returns null.
I think something is wrong with the building of the jar but in IntelliJ I set the gradle task as 'jar' which did create a working jar with working xml file.
Am I doing anything wrong regarding the building or is there anything wrong with the xml file?
There is a difference between the two functions:
https://stackoverflow.com/a/20069798/5387592
You should precise: in which circumstances, the results differ : after create new jar, or after launching again.
1 check if the file is really embedded in the jar: change .jar to .zip and check inside
2 check the real path.
In Eclipse for example, you put in a folder, but this folder doesn't appear in resulting jar
3 use advices: take absolute path as you do
I've run .jar files before, but I've encountered a "different" situation, and I'm not sure what to do. I'd appreciate if someone could help me out.
Previously, I programmed with Java 6 and Eclipse Juno exported all my programs to runnable jar files. I'd get a .jar file that I could run by just double clicking on it. The files always looked something like this (note the jar file icon):
Recently, I wrote a program in Java 8 with Eclipse Luna (Release 4.4.0) and exported it to a runnable jar file, and I got something different (note the different file icon):
It no longer runs when I double click it. Instead, my computer uncompresses the jar, as it would a zip file. I tried running it from terminal. I cd'd to the directory and typed
java -jar graph3D.jar
I got the following error message:
Error: Unable to access jarfile graph3D.jar
After uncompressing the jar file, I found a folder named META-INF with the manifest file, MANIFEST.MF in it. It was the only file that seemed to resemble an executable file. Do I have to do something with that?
Could someone explain how I can run the second jar file graph3D.jar? Is it something new with Java 8, or something different about Eclipse Luna, or something else?
(Both programs run fine in Eclipse, by the way)
Thanks for your time and help.
Edit:
Below was the dialog box Eclipse displayed if anyone is interested.
Selecting "Use .jar;.zip" makes the filename "graph3D.jar;.jar;*.zip" .
Selecting "Use .zip" makes the filename "graph3D.jar;*.zip"
Selecting "Cancel" doesn't let you go forward.
You'd have to manually delete the extra file extension.
Somehow when you exported the file, the filters for the file dialog box (*.jar;*.zip) got attached to the filename, which is graph3D.jar;*.jar;*.zip, not graph3D.jar. Java can't find it because it doesn't have the name you supplied. Rename the file and pay close attention next time you export; either you fat-fingered something, or you're triggering a significant bug that needs fixing.
I recommend that you will access the build folder after you've built your project on the IDE under your project folder (in your workspace) and copy both the libraries folder and the .jar and post them wherever you want the program to be "installed", you'll then have an executable jar that should run smoothly without problems, just as I said don't forget the lib folder.
I think there is nothing new in Java 8 related with the running jar, I guess you need to check the the Eclipse export issues, it seems your classes are missing from your second jar file.
I have tomcat installed at "C:\Program Files\apache-tomcat-7.0.27"
I have eclipse installed at "C:\Program Files\eclipse"
And I have the workspace located at "C:\workspace"
I'm using "Java perspective", created a "Java Project" with the default output folder as "helloWorld/web/WEB-INF/classes".
The structure of the project goes like this:
-helloWorld
---src
-----servlets
-------hello.java
-------world.java
---web
-----WEB-INF
-------jsp
---------hello.jsp
---------world.jsp
-------lib
-------web.xml
---helloWorld.xml
---record.txt
doPost() in hello.java generate a random number, and write it to a text file "record.txt".
doPost() in world.java open the text file "record.txt" and read a number.
The system is working, but what I originally put in the record.txt file in eclipse project never get changed, and I'm sure that what world.java read from the file is exactly what hello.java generated.
I checked "C:\Program Files\apache-tomcat-7.0.27\work\Catalina\localhost\helloWorld", and only jsp files are there.
I then tried restart tomcat and reload and even undelopy and deploy again, but the previous generated number is still there. I didn't try restart computer.
My question is where is the record.txt file? It is definitely not the one in the eclipse project.
If you use a relative file path in your Java code then it will not be relative to your webapp it will be relative to where the process running Tomcat was started. Therefore you might find your file in the Tomcat bin directory or somewhere similar.
If you want to create a file relative to your webapp then you need to obtain the path to your webapp, which you can do by calling getServletContext().getRealPath("") in your Servlet.
Eclipse deployed projects are run in temp folder. the path looks some thing like this... tmp0/conf/catalina/localhost/projectname.....context.xml of servers might help you in this..
i suggest u should go for an absolute path....i faced same problem while using eclipse...That time i had to even provide the link to the user...
Check your webapps folder for a folder with the same name as your project.
C:\Program Files\apache-tomcat-7.0.27\webapps
Here you will find the exact folder structure and your record.txt file as well.
Hope this helps!