The directory structure is as follows
Test
app
libs
commons-codec.jar
guava-r09.jar
sqlcipher.jar
src
main
java
jniLibs
armeabi
libdatabase_sqlcipher.so
libsqlcipher_android.so
libstlport_shared.so
x86
libdatabase_sqlcipher.so
libsqlcipher_android.so
libstlport_shared.so
Now, it keeps giving me the Exception java.io.FileNotFoundException: icudt46l.zip no matter where I put it.
I have tried putting assets\icudt46l.zip in Test\app, Test\app\libs, Test\app\src, Test\app\src\main, Test\app\src\main\jniLibs but the Exception persists.
Where should I put the folder "assets" which contains the zip file?
Now, it keeps giving me the Exception java.io.FileNotFoundException: icudt46l.zip no matter where I put it.
Given your project structure, put it in Test/app/src/main/assets/. IOW, you will need to create an assets/ directory that is a peer of your java/ directory, and put the ZIP file in there.
Related
This question already has answers here:
Java, reading a file from current directory?
(8 answers)
FileNotFoundException even when the file is there
(3 answers)
Closed 9 days ago.
I'm not entirely sure if this is a Java issue or an IntelliJ issue, but I had a quick question about my File not being found via a path. My Main class is in the same directory as my input.txt file.
I though I should be able to do File file = new File("./input.txt"), except I get FileNotFoundException. When I do something like File file = new File("src/input.txt"), it does work.
I get that this may be a solution, but if I try to run this code outside of IntelliJ without a src directory, this will lead to an error where the File can't be found.
Is there any reason why I can't just do ./input.txt to specify that the File is in the same directory as the Main file?
That's because in IDEA, the working directory of your program is the root directory of your project, not src by default. Therefore, you can access the file by ./src/input.txt or src/input.txt, not input.txt. By the way, it's bad practice to put your resources in src, because it gets mixed with your source code, and it won't be included in the JAR file, which means you cannot access it when your project is packaged as a JAR file. If you are using maven, you can put input.txt inside the resources folder(src/main/resources), and access it by Main.class.getResource("/input.txt")(gives you a URL) or Main.class.getResourceAsStream("/input.txt")(gives you a InputStream) depending on your needs. So that the resources will always be available, no matter you are running the program in IDEA or from a JAR.
FileNotFoundException occurs when the file doesn't exist at the specified location.
File file = new File("./input.txt"), the current working directory (.) is being used as the file location.
If you are running the code from an IDE, the current working directory is usually the project root directory, not the directory where the code file is located.
So, if the input.txt file is located in the same directory as your code file, then you should use the relative path to it.
File file = new File("src/input.txt"), you are specifying the absolute path to the input.txt file, which is inside the src directory.
This is why it works, as the file is guaranteed to exist at that location.
If the file is located in the same directory as your code file, you can use a relative path, and if it's in a different directory, you should use an absolute path to it.
The issue:
We have a jetty web-app, and in the application-code, I am trying to access a zip within a jar in classpath. Here's the jar in the libs folder:
/path/to/app/x.x.0-SNAPSHOT/apps/libs/my-model.jar
where my-model.jar is really just an empty folder with model.zip file inside it. If I extract this jar, I get johnsnow/mymodel.zip
My application code tries to access this zip as:
getClass().getResourceAsStream("johnsnow/mymodel.zip")
but of course, I don't get a proper handle to this resource and wind up getting a nullpointer exception. What am I doing wrong? Shouldn't I be able to access a file within a jar file in classpath using the getClass().getResourceAsStream() method?
Footnote:
Because model.zip was too large, we decided against shipping it with the code base. Thus we pushed it into a nexus repository, and reference the jar via a gradle compile dependency as follows:
compile "com.company.group.nlp:my-model:1.0#jar"
The fact that building the distribution pulls this jar, and puts it in apps/libs tells me that gradle does its part (of downloading the dependency to a classpath). The issue remains that I can't seem to find a way to access mymodel.zip inside my-model.jar
Try adding a slash to the file path:
getClass().getResourceAsStream("/johnsnow/mymodel.zip");
It will tell java to start looking for the class from the root folder, not from the current class package.
I am trying to run program from command prompt
Here is my director structure
In the classes directory i have this structure
In the email folder i have two properties file general-mail-settings.properties and customer-mail-settings.properties
Now when i run the command
D:\vintnes\lasses>java -cp ".;..\dependency-jars\*" com/softech/ls360/integration/BatchImport vintners
Then i get the error that
java.lang.Exception: Email Properties File not found: src\main\resources\email\general-mail-settings.properties (The system cannot find the path specified)
at
...
I tried this to specify path
java -cp ".;..\dependency-jars\*;.\email\*.*" com/softech/ls360/integration/BatchImport customer
But still i am getting the error. I tried ;email\* and \email\*, but still i am getting the error. How can i specify path so program get run?
Thanks
You put a path to src/main/resources in your code somewhere. This is a directory used by Maven builds to hold "resource" files (files that aren't code but that should be copied into the finished artifact, like configuration files or media). The contents of src/main/resources are copied directly into the root of the artifact as-is, so in this case, the email directory is copied straight into your classes directory. Remove the src/main/resources part of the path from your properties lookup.
I´m currentl trying to get into JavaFX 2.0 but I can´t get .css Stylesheets to work within my application. I´ve followed the guide at http://docs.oracle.com/javafx/2/get_started/css.htm#BABBGJBI but whenever I try to import a .css file via
scene.getStylesheets().add(Login.class.getResource("loginform.css").toExternalForm());
I get the following Error:
Exception in Application start method
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:399)
at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:47)
at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:115)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
at Login.start(Login.java:68)
at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:315)
at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:174)
at com.sun.javafx.application.PlatformImpl$3.run(PlatformImpl.java:141)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29)
at com.sun.glass.ui.win.WinApplication$2$1.run(WinApplication.java:62)
... 1 more
The css path is located in my eclipse project path:
C:\Users\UserName\Dropbox\Documents\Eclipse\FirstExamples\loginform.css
I´d appreciate any help, I can´t get it working, not with absolute paths nor with relative ones like shown in the example.
Check the compiled output path of your eclipse project and ensure that loginform.css is in the same directory as Login.class
I´ve solved the problem by putting stylesheets into my src folder not
a very nice solution but going up by ../formm.css doesn´t seem to work
dunno why
You are requesting the css file via the class.getResource method, which will look for the resource in the same location the class is located. When you put the stylesheet in your src folder, then whatever build system you are using is copying the stylesheet to the same directory as Login.class during the build. When you don't place it in that src folder, then the copy is not occuring.
If you don't want to place the css file in the same directory as your Java source, then either set your build system up to copy it across from another directory OR don't use the classloader mechanism when you add a stylesheet, e.g. use an absolute file or http url or a file url based on the user.dir system property (current working directory) instead.
Really though, just placing leaving the stylesheet in the src folder and accessing it in the manner you are doing is fine and it also makes resource location easy when you deploy the app to different environments.
If you use anything as a resource, you have to prefix the name of it with "/" and that resource has to be on the classpath.
I have the setup with a directory e.g. resources, which contains the .css. Then I use this:
scene.getStylesheets().add(Login.class.getResource("/loginform.css").toExternalForm());
The directory 'resources' which contains the .css file is added to the classpath (in Eclipse, for example, just make it be a source folder).
It is however pity that the exception is totally useless; I've noticed that the .css was the problem when I added it for the first time.
I have a problem where I can't seem to link to a xml file, see the layout below:
Folder Name
-Folder
-Folder
-SourceFiles
-packagename
-all my java files
-myXml.xml
Build is where all the class files etc is stored.
src is where the projectFolder is, and within it the java files
Code I am using to link XML File for Synth: SynthDialog.class.getResourceAsStream("synthtest/synthDemo.xml")
Now I want to link to the myXML.xml file in the top-level folder. It would be the PHP Equivelent of ../../Folder/
Thanks
You appear to be attempting to access the file using getResourceAsStream with a relative name. If that is the case, then the resource should be in located in a JAR file or directory on the classpath, and the location will be resolved relative to the FQN of the class.
I can't tell where the ".class" files are located in the tree, or how your classpath is set up, so I can't be more specific.
UPDATED
If you are executing out of that build directory, then your build process needs to copy the XML file to the appropriate place in the build tree so that the class-relative path ends up referring to the file. (Or use a path that starts with "/" so that you don't depend on the classes FQN at all.)
In the long term, you will probably execute out of a JAR file, and the data file will need to be inside it.
Use "getSystemResourceAsStream" instead of "getResourceAsStream" to access files outside of your codebase.