0

I want to implement Following stuff with my java code in eclipse.

i need to edit the .dict file which is in directory of jar file.

my directory structure is like

C:\Users\bhavik.kama\Desktop\Sphinx\sphinx4-1.0beta6-bin\sphinx4-1.0beta6\modified_jar_dict\*WSJ_8gau_13dCep_16k_40mel_130Hz_6800Hz.jar*\dict\**cmudict04.dict**

Text with bold character is my text file name which i want to edit

and text with italic foramt is my .jar file

now how can i edit this cmudict04.dict file which is reside in WSJ_8gau_13dCep_16k_40mel_130Hz_6800Hz.jar\dict\ directory on runtime with java application.

and i want the jar file with the updated file i have edited.

please can u provide me any help?

thnank you in advance.

vikiiii
  • 9,246
  • 9
  • 49
  • 68
BhavikKama
  • 8,566
  • 12
  • 94
  • 164
  • sorry for spelling mistake in directory path above..let me cleary give u...C:\Users\bhavik.kama\Desktop\Sphinx\sphinx4-1.0beta6-bin\sphinx4-1.0beta6\modified_jar_dict\WSJ_8gau_13dCep_16k_40mel_130Hz_6800Hz.jar\dict\cmudict04.dict – BhavikKama Aug 30 '12 at 10:58
  • You can put your clarification in post by editing. – amicngh Aug 30 '12 at 10:59

3 Answers3

1

I would recommend to use java.util.zip.Using these classes you can read and write the files inside the archive .But modifying the contents is not guaranteed because it may be cached.

Sample tutorial

http://www.javaworld.com/community/node/8362

amicngh
  • 7,831
  • 3
  • 35
  • 54
1

You can't edit files that are contained in a Jar file and have it saved in the Jar file ... Without, extracting the file first, updating it and creating a new Jar by copying the contents of the old one over to the new one, deleting the old one and renaming the new one in its place...

My suggestion is find a better solution

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • so how can i do that..can any one put some sample code for that? – BhavikKama Aug 30 '12 at 13:13
  • @BhavikKama do a search for adding files to a zip file, this is a rather common question ;) – MadProgrammer Aug 30 '12 at 19:37
  • @BhavikKama This are just a few I found http://stackoverflow.com/questions/3048669/how-can-i-add-entries-to-an-existing-zip-file-in-java http://www.dzone.com/snippets/adding-files-existing-jar-file – MadProgrammer Aug 30 '12 at 19:47
  • What i want is i have one Folder and i want to convert that whole Folder with the .jar file as the command in dos we write like jar -cf new.jar Folder_name.. – BhavikKama Aug 31 '12 at 05:02
0

I had succeded to edit jar file and wrap it back as it is...with the following code

public void run() throws IOException
    {
      Manifest manifest = new Manifest();
      manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
    //  JarOutputStream target = new JarOutputStream(new FileOutputStream("E:\\hiren1\\WSJ_8gau_13dCep_16k_40mel_130Hz_6800Hz.jar"), manifest);
     // add(new File("E:\\hiren1\\WSJ_8gau_13dCep_16k_40mel_130Hz_6800Hz/"), target);
      JarOutputStream target = new JarOutputStream(new FileOutputStream("C:\\Users\\bhavik.kama\\Desktop\\Sphinx\\sphinx4-1.0beta6-bin\\sphinx4-1.0beta6\\modified_jar_dict\\WSJ_8gau_13dCep_16k_40mel_130Hz_6800Hz.jar"), manifest);
         add(new File("C:\\Users\\bhavik.kama\\Desktop\\Sphinx\\sphinx4-1.0beta6-bin\\sphinx4-1.0beta6\\modified_jar_dict\\WSJ_8gau_13dCep_16k_40mel_130Hz_6800Hz/"), target);

      target.close();
    }

    private void add(File source, JarOutputStream target) throws IOException
    {
      BufferedInputStream in = null;
      try
      {
        if (source.isDirectory())
        {
          //String name = source.getPath().replace("\\", "/");

            if(isFirst)
            {
                firstDir = source.getParent() + "\\";
                isFirst = false;
            }
            String name = source.getPath();
            name = name.replace(firstDir,"");
          if (!name.isEmpty())
          {
            if (!name.endsWith("/"))
              name += "/";
            JarEntry entry = new JarEntry(name);
            entry.setTime(source.lastModified());
            target.putNextEntry(entry);
            target.closeEntry();
          }
          for (File nestedFile: source.listFiles())
            add(nestedFile, target);
          return;
        }
        String name = source.getPath();
        name = name.replace(firstDir,"").replace("\\", "/");
        //JarEntry entry = new JarEntry(source.getPath().replace("\\", "/"));
        JarEntry entry = new JarEntry(name);
        //JarEntry entry = new JarEntry(source.getName());
        entry.setTime(source.lastModified());
        target.putNextEntry(entry);
        in = new BufferedInputStream(new FileInputStream(source));

        byte[] buffer = new byte[1024];
        while (true)
        {
          int count = in.read(buffer);
          if (count == -1)
            break;
          target.write(buffer, 0, count);
        }

        target.closeEntry();
      }
      finally
      {
        if (in != null)
          in.close();
      }

    }
BhavikKama
  • 8,566
  • 12
  • 94
  • 164