File in executable jar cannot be found when running on AWS EC2 - jar

I have a .jar file executing on a aws ec2 instance which contains the following code:
List<String> lines = FileUtils.readLines(new File("googlebooks-eng-all-1gram-20120701-k"));
the file exists in projectname/res and also in /projectname directly. I included /res in the build path. Also I see that the file exists inside the jar file at the root if I export the .java file in eclipse.
If I run the jar localy on my pc it works fine. But if I run it on a ec2 instance it says:
java.io.FileNotFoundException: File 'googlebooks-eng-all-1gram-20120701-k' does not exist
How can that be?

On your PC it is reading from the actual file on the filesystem - that is what new File means - a file on the filesystem.
To access a resource in a jar file you need to call getResourceAsStream or something similar instead.

Related

Unable to read xml file when using exe4j to package jar into exe

Recently I encountered a problem when using exe4j to package the jar into exe. My xml configuration file is placed in the same directory of the exe, but after the package is completed, the exe will look for the xml file from the temporary folder.
These are the instructions in the exe4j help documentation
For some applications (especially GUI applications) you might want to change >the working directory to a specific directory relative to the executable, for >example to read config files that are in a fixed location. To do so, please >select the Change working directory to: checkbox and enter a directory relative >to the executable in the adjacent text field. To change the current directory >to the same directory where the executable is located, please enter a single >dot.
The error when i running the exe :
[ERROR] In Log's init,cann't read config file, file=/C:/Users/**/AppData/Local/Temp/e4jEA8.tmp_dir1543543191//sys_log.xml
Why is the program not looking for this xml from the exe's sibling directory, and how can I find it from this directory?
If you are using the "JAR in EXE" mode, use
System.getPrpoerty("install4j.exeDir")
to get the parent directory of the executable.

Accessing a JSON file in tomcat war and reading it

I want to read a json file placed in resources/data/info.json in my webapp. Got to know that need to use Convertor.class.getResourceAsStream("\\WEB-INF\\classes\\data\\Address.json"); where Convertor is my util class for some reason am unable to read the file and i see the InputStream returning null and i see the below exception :
com.fasterxml.jackson.databind.exc.MismatchedInputException: No content to map due to end-of-input
at [Source: UNKNOWN; line: 1, column: 0]
at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:59)
at com.fasterxml.jackson.databind.ObjectMapper._initForReading(ObjectMapper.java:4133)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3988)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3058)
at com.wf.hrca.util.Convertor.unMarshal(Convertor.java:84)
resources is a folder of your source project on your development machine. Tomcat doesn't care about how your source project looks like. And by the way, when you'll deploy the application in production, there won't be any source project on the machine.
All Tomcat cares about is the war file you deploy. Inside this war file, the WEB-INF/classes directory, along with all the jar files under WEB-INF/lib, constitute the classpath of the application.
Convertor.class.getResourceAsStream(), as the javadoc explains (but you really need to read it to know) expects a /-separated path. If the path starts with a /, then the path starts at one of the roots of the classpath (i.e. / refers to WEB-INF/classes, and to the root of each jar file of WEB-INF/lib).
So, find where the json file is located inside the war file. If it's under /WEB-INF/classes/data/info.json, then you should use
Convertor.class.getResourceAsStream("/data/info.json");

hive looks for hdfs private directory for jar

How does add jar work in hive? when I add a local jar file,
add jar /users/course/jars/json-serde-1.3.1.jar;
hive query fails and says it could not find the jar in hdfs, same directory.
Job Submission failed with exception 'java.io.FileNotFoundException(File does not exist: hdfs://localhost:9000/users/course/jars/json-serde-1.3.1.jar)
Then I put the jar into hdfs, add jar using that hdfs filepath.
add jar hdfs://localhost/users/course/jars/json-serde-1.3.1.jar;
Now, hive query says
File does not exist: hdfs://localhost:9000/private/var/folders/k5/bn104n8s72sdpg3tg7d8kkpc0000gn/T/a598a513-d7c9-4d55-9280-b6554487cac7_resources/json-serde-1.3.1.jar
I have no idea why it keeps looking for the jar in wrong places.
I believe Hive looks for the JAR locally, not on HDFS.
So if my home directory on the gateway server is
pwd
/home/my_username/
And the JAR is sitting locally at:
/home/my_username/hive_udfs/awesomeness.jar
Then I'd go into the hive shell and run:
add jar /home/my_username/awesomeness.jar
At least, that works for me in my environment. HTH. Good luck! :)

sqlite database connectivity in servlet

I am writing jdbc code in Servlet
But I am getting an error
java.lang.ClassNotFoundException: org.sqlite.JDBC
I have included a sqlitejdbc-v056.jar file
Still I am getting an error.
If i write the same code in separate java file and run it as a java application,
it works properly but it is not working on server
p.s.- I am using Weblogic server.
I have found a solution on my problem.
We have to add the .jar file in lib folder in WEB-INF folder in our workspace
-"Workspace/WebContent/WEB-INF/lib/"
Also we need to include it in 'plugins' folder of eclipse.
-"eclipse/plugins/"

Can I run a jar file from shared folder of remote system just by double clicking from my system?

I shared a folder containing a .bat file which calls/run a jar file from same folder. I need to run that .bat file(present on remote system's shared folder) from my system. It run successfully if that folder is on my system but if i run the same file from shared folder of remote system it gives message "cannot access jar file."
Please help me.
Thanks in advance.

Resources