Hi.
I'm programming this Java app on NetBeans 7 to deploy it via Web Start and is all going well.
I'm developing on Windows but the jnlp, jar etc. are to reside on a unix box. It runs well on unix too.
At the moment my app reads a text file and display each line at a time. Locally it works just fine with my text file on the right location.

Problem is on unx, it can't find my text file or its path.
This is the folder in Unix where my text file (projects) is located)
# pwd
/disk/ip/ipgal

My code:

try {
            File file = new File("/disk/ip/ipgal/projects");

            FileReader input = new FileReader(file);
            BufferedReader bufRead = new BufferedReader(input);
            String line;    // String that holds current file line
            int count = 0;  // Line number of count 
            // Read first line
            line = bufRead.readLine();
            count++;

            // Read through file one line at time. for each project, get the path

            while (line
                    != null) {
                String curr = line.substring(17);
                JOptionPane.showMessageDialog(null, curr, "Line read from file", WIDTH);
                line = bufRead.readLine();
                count++;
            }
       } catch (IOException e) {
            // If another exception is generated, print a stack trace
            JOptionPane.showMessageDialog(null, "Unable to open Project file. (IO)", "Error: Contact Programmer.", WIDTH);
            e.printStackTrace();
             } try {
                StringWriter sw = new StringWriter();
                PrintWriter pw = new PrintWriter(sw);
                e.printStackTrace(pw);
                JOptionPane.showMessageDialog(null, sw.toString(), "Error: Contact Programmer.", WIDTH);
            } catch (Exception e2) {
            }
       }

Exception shows:
io.FileNotFoundException \disk\ip\ipgal\projects (The system cannot find the path specified)

Tried this to find running dir, but only got the local c: path:

File dir1 = new File(".");
            JOptionPane.showMessageDialog(null, dir1.getCanonicalPath(), "Error: Contact Programmer.", WIDTH);

Also, tried accessing a file via URL with good results, but the file i need to read is not on the www folder, but in /disk/ip/ipgal/<myTextfile>

Thank you in advance for any help on this.

Regards.

Carlos


This is the file location in Unix:
# pwd
/disk/ip/ipgal

Recommended Answers

All 5 Replies

If this code runs on a Unix box, it occurs to me that one of the directories or the file itself might not be readable by the "user" the process is executing as. Try giving everyone read access to the directories and file.

On the other hand, you did mention JNLP: If the code is in the JAR file downloaded through JNLP, then the code is executed on the client, where the browser is running, not on the Unix web server. The file might not exist on all clients. In that case, consider putting the file (if it's fixed and constant) in one of the Java source code package directories, so it will be included in the JNLP-downloaded JAR, and use the ClassLoader getResourceAsStream method to read it.

Thank you Jeff:

I've just verified the file has all access granted; actually the error message does not mention access denied :-(.
As for the jnlp, I'm reading a file that is constantly changing, so I have to access it now and then, read each line and do something.

My scenario is this:
- Develop java app on windows netbeans 7. The app do read the local file ok.
- To deploy, I moved all contents from /dist folder to a wwwdir on my apache server (unix). From there, my app needs to read a file in a location I'm harcoding (see line 2):

// READ PROJECTS FILE ETC.
            File file = new File("/disk/ip/ipgal/projects");

            FileReader input = new FileReader(file);
            BufferedReader bufRead = new BufferedReader(input);
            String line;    // String that holds current file line
            // do something

            // Read through file one line at time. for each project, get the path
            line = bufRead.readLine();
            
             bufRead.close();
        } catch (ArrayIndexOutOfBoundsException e) {
            /* If no file was passed on the command line, this expception is
            generated. A message indicating how to the class should be
            called is displayed */
            JOptionPane.showMessageDialog(null, "Unable to open Project file. (IOOB)", "Error: Contact Programmer.", WIDTH);

        } catch (IOException e) {
            // If another exception is generated, print a stack trace
            JOptionPane.showMessageDialog(null, "Unable to open Project file. (IO)", "Error: Contact Programmer.", WIDTH);
            e.printStackTrace();
            try {
                StringWriter sw = new StringWriter();
                PrintWriter pw = new PrintWriter(sw);
                e.printStackTrace(pw);
                JOptionPane.showMessageDialog(null, sw.toString(), "Error: Contact Programmer.", WIDTH);
            } catch (Exception e2) {
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Final error: " + e.getMessage(), "Final Error", WIDTH);
            e.printStackTrace();
        }

Teh file is there, and readable:
-bash-3.00$ pwd
/disk/ip/ipgal
-bash-3.00$ ls -lrt projects
-rwxrwxrwx 1 2017 progrmr 573 Aug 1 16:10 projects
-bash-3.00$


However I still get io.FileNotFoundException.

Any help appreciated.

Regards.
Carlos

If this code runs on a Unix box, it occurs to me that one of the directories or the file itself might not be readable by the "user" the process is executing as. Try giving everyone read access to the directories and file.

On the other hand, you did mention JNLP: If the code is in the JAR file downloaded through JNLP, then the code is executed on the client, where the browser is running, not on the Unix web server. The file might not exist on all clients. In that case, consider putting the file (if it's fixed and constant) in one of the Java source code package directories, so it will be included in the JNLP-downloaded JAR, and use the ClassLoader getResourceAsStream method to read it.

and if you change

File file = new File("/disk/ip/ipgal/projects");

to

File file = new File("\\disk\\ip\\ipgal\\projects");

network access required

File file = new File("\\\\disk\\ip\\ipgal\\projects");

As for the jnlp, I'm reading a file that is constantly changing, so I have to access it now and then, read each line and do something.

My scenario is this:
- ...
- To deploy, I moved all contents from /dist folder to a wwwdir on my apache server (unix). ...

If the code that you're showing us is in the JAR that is deployed using JNLP, then I think I know what the problem is: Java code in a JAR run through JNLP runs on the client, not the server. The file is on the server, not on the client. This suggests two possible solutions:

  1. Deploy the text file to the server in the WAR/EAR but outside the JAR, like '.html' files. The client Java code would read the file through HTTP using a URL.
  2. The server can expose a web service that the client can call to fetch the data. [This is what I have done most often with JNLP.]

Thank you Jeff;

As a beginner, it seems very cumbersome having to implement WAR files. Aside from reading the text file I mentioned, we may need to re-create that same file, along with reading a bunch of other text files, most of which can be recent, as in new, (the first file has location for the bunch of text files i need to access). Additionally, I'll need to create a whole new text file so we basically need carte blanche to read and write text files. WAR files seems like a constraint in this case.

I could use a WAR file if only one file were to be accessed, but for many files, what options do i have?

Also, I could leave aside the whole Web Start thing if there's no way around this and still use java (need to learn it somehow) in a different manner.

Thanks everybody for any help.

Carlos

If the code that you're showing us is in the JAR that is deployed using JNLP, then I think I know what the problem is: Java code in a JAR run through JNLP runs on the client, not the server. The file is on the server, not on the client. This suggests two possible solutions:

  1. Deploy the text file to the server in the WAR/EAR but outside the JAR, like '.html' files. The client Java code would read the file through HTTP using a URL.
  2. The server can expose a web service that the client can call to fetch the data. [This is what I have done most often with JNLP.]
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.