14 Replies Latest reply: Jan 13, 2007 1:51 AM by 807599 RSS

    How to map a database file kept inside the jar file

    807599
      I am running my program from NetBeans 5.0. Inside the src path I have made a folder SupportingFiles inside which am keeping my database file data.txt. When I do Build the Main Project, this is getting included in my jar file. But could anyone please tell me how do I map in my program to this database file data.txt.

      For instance if the database file is in D:/temp/data.txt, then in my program I map it as
      File dstfile=new File("D:/temp/data.txt");
      but I do not want the database file data.txt to be kept in any particular path and it should be a part of my jar file.
        • 1. Re: How to map a database file kept inside the jar file
          masijade
          Using getResourceAsStream from Class/ClassLoader you can read the file easily enough, but don't expect to be able to write to it.

          Read the API for that method.
          • 2. Re: How to map a database file kept inside the jar file
            807599
            Please tell me how do I go more further with my coding
            InputStream is=getClass().getResourceAsStream("data.txt");
            File dstfile=new File(is);
            Process procViewIe =Runtime.getRuntime().exec("C:\\Program Files\\UltraEdit\\UEDIT32.EXE \""+dstfile+"\"");
            At
            File dstfile=new File(is);
            it's throwing an error for
            new File(is);
            as cannot find symbol

            I need to open the data.txt kept in my jar file in ultraedit

            Message was edited by:
            Simmy
            • 3. Re: How to map a database file kept inside the jar file
              masijade
              Because, getResourceAsStream is already "opening the file". If you wish to use the data in the file outside of the program, you are going to need to read from the jarfile, write it out to someplace else, then use that copy. You won't be able to use it as shown in your example from inside the jarfile. If it must be used this way, then as mentioned, read it out, write it someplace else, and use that copy.

              As I said in an earlier post, however, you won't be able to update the original file inside of the jarfile, at least not without rebuilding the entire jarfile.
              • 4. Re: How to map a database file kept inside the jar file
                807599
                Could you please tell me more specifically how do I go about with this.

                Currently my coding for opening a database file data.txt kept inside a particular path is:
                private void ultraButActionPerformed(java.awt.event.ActionEvent evt)
                {
                     java.awt.EventQueue.invokeLater(new Runnable() 
                     {
                          public void run() 
                          {
                               try
                               {
                                    File dstfile=new File("C:\\temp\\data.txt");
                                    Process procViewIe =Runtime.getRuntime().exec("C:\\Program Files\\UltraEdit\\UEDIT32.EXE \""+dstfile+"\"");
                               }
                               catch(IOException ioe)
                               {
                                    javax.swing.JOptionPane.showMessageDialog(Main.this,ioe.getMessage(),"IO EXCEPTION",JOptionPane.OK_OPTION);
                               }
                          }
                     });
                }
                The above coding is working fine if I have the database file in a particular path. But I do not want this database file kept in a particular path, hence I have this file embedded inside my jar file. Could you please tell me the upadations to my above coding to open the database file data.txt kept inside my jar file to open in ultraedit.
                • 5. Re: How to map a database file kept inside the jar file
                  masijade
                  // Replace
                  File dstfile=new File("C:\\temp\\data.txt");
                  Process procViewIe =Runtime.getRuntime().exec("C:\\Program Files\\UltraEdit\\UEDIT32.EXE \""+dstfile+"\"");
                  // With this
                  InputStream is=getClass().getResourceAsStream("data.txt");
                  File dstfile=File.createTempFile("data", "txt");
                  FileOutputStream fos = new FileOutputStream(dstfile);
                  int b;
                  while((b = is.read()) != -1) {
                    fos.write(b)
                  }
                  fos.close();
                  Process procViewIe =Runtime.getRuntime().exec("C:\\Program Files\\UltraEdit\\UEDIT32.EXE \""+dstfile.getAbsolutePath()+"\"");
                  Once again, however. This will, obviously, not update the file inside of the jarfile. That can only be done by rebuilding the jarfile which is not something that can be done pratically inside of the still running Java process using that jarfile.

                  So, doing this will allow them to view the file, but changing it will achieve, essentially, nothing.

                  data.txt must appear then in the same package as this class. If not then add the package path (in directory format with a leading / ) to the file name in getResourceAsStream.
                  • 6. Re: How to map a database file kept inside the jar file
                    807599
                    Thanks a lot for the help. My program is working fine. I was trying for this from yesterday.
                    • 7. Re: How to map a database file kept inside the jar file
                      807599
                      The code given by you works fine to open the database file test.txt in ultraedit, but in the same way if I try to access a batch file kept inside the jar file, its throwing an error as Cannot find the file dstfile (or one of its components). Make sure the path and filename are correct and that all required libraries are available
                      InputStream is=getClass().getResourceAsStream("test.bat");
                      File dstfile=File.createTempFile("test", "bat");
                      FileOutputStream fos = new FileOutputStream(dstfile);
                      int b;
                      while((b = is.read()) != -1) 
                      {
                           fos.write(b);
                      }
                      fos.close();
                      //String pattern="cmd /c start /MIN D:/temp/test.bat ";//currently this code is working fine
                      //Process procPattern=rt.exec(pattern,null,fil);
                      String pattern="cmd /c start /MIN dstfile";
                      Could you please tell me where am I wrong.
                      • 8. Re: How to map a database file kept inside the jar file
                        masijade
                        you want to use the method dstfile.getAbsolutePath( ) not the string dstfile.
                        // Change
                        String pattern="cmd /c start /MIN dstfile";
                        // to
                        String pattern="cmd /c start /MIN " + dstfile.getAbsolutePath();
                        And I'm sorry, but I had to laugh when I saw that. I don't mean to be mean, you have to admit, it's funny. ;-)
                        • 9. Re: How to map a database file kept inside the jar file
                          807599
                          I tried with the code you gave me
                          String pattern="cmd /c start /MIN " + dstfile.getAbsolutePath();
                          but its not working instead the Open With window is popping up with the error Click the program you want to use to open ?test138303bat?.
                          If the program is not in the list, click other.

                          Either I click ok, cancel or other?, the necessary process is not happening from the batch file.
                          • 10. Re: How to map a database file kept inside the jar file
                            masijade
                            Sorry. On the createTempFile line change "bat" to ".bat". Also, on the other createTempFile line change "txt" to ".txt". Notice the dot in the changes and then look at filename in your error message again and you will see the reason for this.
                            • 11. Re: How to map a database file kept inside the jar file
                              807599
                              Thanks a lot, its working.
                              • 12. Re: How to map a database file kept inside the jar file
                                807599
                                From the program I am calling a batch file test.bat kept inside a jar file. This batch file calls an exe file test.exe. From my code, this exe file found in the batch file makes a temp file in C:\Documents and Settings\author\Local Settings\Temp, but I need this exe file to a particular path, say d:\temp\workingArea. Could you please tell me what updations I need to do in my code.
                                InputStream is=getClass().getResourceAsStream("/SupportingFiles/test.bat");
                                File dstfile=File.createTempFile("test", ".bat");
                                dstfile.deleteOnExit();
                                FileOutputStream fos = new FileOutputStream(dstfile);
                                int b;
                                while((b = is.read()) != -1) 
                                {
                                     fos.write(b);
                                }
                                fos.close();
                                String pattern="cmd /c start /MIN " + dstfile.getAbsolutePath();
                                Please don't tell me to run the exe file directly in the program, because it was not possible due to some other reason. So I inserted the exe file inside a batch file. And both the exe file and the batch file is kept in the jar file.
                                • 13. Re: How to map a database file kept inside the jar file
                                  masijade
                                  I assume, what you want to know is how to extract test.exe from the jarfile, right?.

                                  Then, change the name in the getResourceAsStream and instead of using File.createTempFile("name") use new File("name") with name being the exact place and name you wish to use to copy out the exe to.

                                  Remember though, that when you use an exact name, that you need to be careful, because of two instances happen to be running they are liable to walk all over each other. That was the reason for using createTempFile before.
                                  • 14. Re: How to map a database file kept inside the jar file
                                    807599
                                    Thanks a lot for helping me on this again.

                                    Message was edited by:
                                    Simmy