java program in eclipse to read text file in command line - java

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.

Related

Eclipse - Access denied when trying to add files to Program Arguments

I have a folder of files that I am trying to add to the program arguments in eclipse, but when I try to execute the program I get the error:
java.io.FileNotFoundException: [path] (Access is denied)
I am pasting the path into the program arguments field under the arguments tab in run configuration. So far I've tried putting a folder in the project directory, my documents and the desktop but it keeps saying "Access is denied." I am doing this all on my laptop where I am the administrator. Thanks for any help.
Right click on your folder and set the correct permission.
I put the files in the directory with the src folder and it worked. Note, you must put the file extension in the arguments field or it won't work.

Loading a file (externally)

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.

Eclimd: Can't locate files when run from vim

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.

Where does tomcat store txt files used by web applications

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!

How to enable the (JAR) file to access an external folder that is needed in the application?

Please, My Java application is suppose to access an external folder which contains some other files. It was working perfectly under the Netbeans IDE but not when i run the (jar) file alone. I thought including the external desired folder within the same path as the jar file will solve the issue but sadly it gave me the same results.
Any ideas or suggestions are greatly appreciated!
Thanks in Advance!
P.S: I am using the following command: java -jar MyJarFile.jar
[Added]
My File structure:
In the main folder of the project i have the following:
build
build.xml
Data: Inside this folder is my log file (log.dat).
dist: Inside this folder is my (.Jar) file.
manifest.mf
nbproject
README
src
The code to access the file is written as follows:
File logs_File = new File("Data" + File.separator + "log.dat");
Again within my NB it is working this way but not when i lunch the .Jar file.
[Updated]
Up to know, i have been trying the following:
The file to access the file looks as follows now:
File logs_File = new File(this.getClass().getResource("/Data/log.dat").toString());
Then i copied the Data folder to following directories:
src
build/classes/
dist
and in the main directory
And still it is not seeing the file. Though this time not even netbeans is able to locate the file.
Thanks in advance for your help
Without seeing your code, it's hard for someone to give you a precise answer to your question. The hint that you provided about correct behavior within the IDE suggests to me that you may be using a relative path when accessing your file? Have you tried to print out the path you're attempting to access when it fails?
EDIT:
Try calling getAbsolutePath() on your file, and printing the results to STDOUT. Then go look in your file system to see if the file exists.
You need folllwing structure and code for your project
Well the answer is as follows:
I printed the getAbsolutePath(); of my file then i placed my (Data) folder inside the (dist) folder with the (JAR) file. I had also to allow the (jar) file to be accessible by all users and not just the administrative one.
[Topic Solved]
Thanks for the community and all your help and concern!
Kind Regards,

Categories