Eclimd: Can't locate files when run from vim - java

I have an Eclipse project:
myProject/
|--src/
| |--MyClass.java
|
|--bin/
|--MyClass.class
|--data.dat
MyClass reads from data.dat. When I edit MyClass.java and run using :Java from within vim, I get a bunch of errors:
java.io.FileNotFoundException: data.dat (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:120)
at java.io.FileInputStream.<init>(FileInputStream.java:79)
If I navigate to myProject/bin and run the classes there using java MyClass, I'm able to read the data.dat file.
Where do I set the location of the files I'm trying to open so that they're relative to the compiled Java, not relative to my *.java files? It doesn't seem to be in the .classpath file.
As a secondary question: I'm having issues finding the answer to this because I don't quite know what to search for. java runtime file locations doesn't work. What keywords describe this problem so I can find the answer for myself?

This might be because the default path isn't the bin folder but the project folder itself. Try to move your file to the project folder or change its path within the program.
You could also pass the file via commandline arguments but this can cause some other problems as you might already know.

Related

Java FileNotFoundException in jar file

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

Issues with taking args in command prompt and fileNotFoundExceptions

I have a few questions because it seems the basis of my problems is I'm not understanding how to properly run my java class files from the command prompt and have them take arguments.
I have my project folder 'ProjectDNA' and within this directory is my commands.txt that I would like my class to call as an argument.
ProjectDNA\src\package\dna\sequencer is the class I am trying to call.
My issue is I am able to compile the sequencer in a command prompt from ProjectDNA\src\package\dna, but only execute it from
ProjectDNA\src
This only brings in more confusion when the .txt file I need it to process as an arg is in ProjectDNA\commands.txt
After going through some other similar questions here. here, here, and looking at oracles documentation I'm not 100% on how I am supposed to efficiently compile,run, and take the txt file as an argument. Where should I be calling these commands and how should the syntax be in the command prompt? When I run from my src file:
java -cp . package.dna.Sequencer commands.txt
I receive
java.io.FileNotFoundException: commands.txt (The system cannot find the file specified)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
at package.dna.Sequencer.process(Sequencer.java:55)
at package.dna.Sequencer.main(Sequencer.java:113)
Any help would be greatly appreciated.
You've got a couple of different concepts here. One is how Java deals with the classpath and the other is the default location that a Java program will look for a file.
When you use the java command and supply the classpath setting (-cp in your example) you are telling Java where the package(s) you want on the classpath are located. In your example you are saying that the packages are located in the current directory (the . means current directory). You then give the fully qualified class name to run (e.g. with the main() method). At this point Java starts running and loads the requested class, executing the main() method. The directory where you start the Java program is considered the current working directory.
When you create the FileInputStream and give it a file name it will look for that file in the current working directory. You can supply a fully qualified path to the file instead of just a name. You can also use a relative path. For example, in your case you could give the command as:
java -cp . package.dna.Sequencer ..\commands.txt
The .. means the parent directory above the current working directory. This would cause the FileInputStream to look for the commands.txt file in the ProjectDNA directory above the src directory (where you started the Java program).
Alternatively you could start the program from the ProjectDNA directory and use this command:
java -cp src package.dna.Sequencer commands.txt
In this case your current working directory will be ProjectDNA (containing your commands.txt file as well as the src directory). The -cp src tells Java that the src directory should be on the classpath (e.g. class files will be found in packages under that directory).

What is the proper way to deploy Java project that has JSON files?

I am trying to deploy the program by making it into an executable jar file.
When I run the program directly from IDE (IntelliJ), everything is fine. But after I make jar file out of my project, when I run that jar file, I get errors like:
java.io.FileNotFoundException: src\JsonFiles\Words.json (The system cannot find the path specified)
Don't use relative paths to src folder, open an input stream to the file where it's located relative to a class:
InputStream in = SomeClass.class.getResourceAsStream("Words.json");
where Words.json and SomeClass.java exists in the same folder/package.

Cannot read resource from Jar File

I am having trouble figuring out this problem and I have tried almost everything.
I would like my program to read two resources from Jar File, and it will run fine through eclipse. However it gives nullpointer exception when I run the jar from command prompt.
Structure:
src/main/java/App.java
src/main/resources/properties/application.properties
src/main/resources/spring/applicationContext.xml
Code:
To read application.properties
properties.load(App.class.getResourceAsStream("/properties/application.properties"));
To read applicationContext.xml
context = new ClassPathXmlApplicationContext("/spring/applicationContext.xml");
Error:
Error while loading
application.properties java.lang.NullPointerException
What could I be doing wrong?
Assuming that these are in a jar file that is available on the classpath at runtime, the first, getResourceAsStream() example is probably failing because you haven't specified the full path within the jar file. (Also, a common failure it to leave off the leading /, but you seem okay there.) E.g.:
properties.load(App.class.getResourceAsStream("/src/main/resources/properties/application.properties"));
However, if you are unsure where (what path) the files have been given in the jar file that was built, try
jar -tvf <your-jar>.jar
to see what is actually in it.
Can't comment on ClassPathXmlApplicationContext; not a Spring developer.
in your structure you show where the .java is, but you´re missing the .class. The path where it resides is the one the classloader will use. There should be your resources
EDIT: to clarify a bit. In your post you mention the path to App.java. but the classloader only cares about App.class, so let´s say you have
\src
|...(App.java and whatever, these will be ignored)
\bin
|
|--App.class
|
\bin\resources\...(properties and whatever, these will be read)
you should make sure you have properties and spring directories under bin.
If you only have the .java in src it will be ignored by the classloader.

java program in eclipse to read text file in command line

I am trying to read a text file in eclipse, I have "run configurations" -> arguments set in eclipse , but it always says
Exception in thread "main" java.io.FileNotFoundException: file.txt (The system cannot find the file specified)
I have put file.txt under the same folder of myclass and in /bin folder
What else I should look into?
Thanks
Set the working or starting directory in the run configuration, then put the file in that directory.
Put it in the folder of the project, not in bin folder. If you want to see what is the base path of the files, you can print:
System.out.println(new File(".").getCanonicalPath());
If you need to read a file from within an Eclipse plugin you are creating, you should treat at as a resource. Check this for an example: http://www.vogella.de/blog/2010/07/06/reading-resources-from-plugin/ Hope that helps.

Categories