简体   繁体   中英

Adding dependent text files to JAR file

I have a JAR file and it depends on 5 different text files. The JAR file read the text files and give the result. The only problem I have is that I want to hide text files so that no one can see it. Kindly suggest me how should I add these text files in-to the JAR package. If someone knows JAR-TO-EXE tool so that the text files are hided in-to the EXE then it is acceptable too.

I suggest you look into Serialization as a possible solution, here's a link.

http://www.tutorialspoint.com/java/java_serialization.htm

I'm not 100% sure it isnt possible to reconstruct your data anyway but... the content of the file will definently be gibberish when you open it (even if you don't wanna use it for this learn it anyways its very useful).

EDIT: I wanted to elaborate abit so you know what it basically is (if you don't)... imagine you have an arraylist containing objects (the data that you need to save, like your own employee class or whatever. Wih serialization you can basically serialize that list to a file and when you wanna load the data simply deserialize it back. The file with the saved data will contain gibberish to anyone who opens it. It's actually alot easier than working with an average file, where you usually have to handle reading each line etc. also remember you have to implement Serializable interface in the classes you need to do it with.

Made you a simple example, below I'm using an extremely simple class called testing which only contains a name:

public class Testing implements Serializable {

    private String name;

    public Testing(String name) {
        this.setName(name);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

Now to serialize an object of this class, remember you can do this with an arraylist of the objects aswell but it won't work in a static context! So you can't test it in a main method fx.

        File file = new File("test.dat");
        if (!file.exists()) file.createNewFile();

        try {
            ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream("test.dat"));
            out.writeObject(new Testing("testobject"));
        } catch(Exception ex) {}


        try {
            ObjectInputStream in = new ObjectInputStream( new FileInputStream("test.dat"));
            Testing loadedObject = (Testing) in.readObject();
            System.out.println( loadedObject.getName() );
        } catch(Exception ex){}

The above code can be tested within a main method if you wanna see it in action, just copy paste it and import the stuff needed.

EDIT: Should mention that I read it is possible to use a simple but secure encryption method by using a wrapper class before serializing the object. But you will have to read up on that, though imo it would be a very elegant solution to use.

You can 'hide' the files in the jar, but they will still be accessible. Just unzipping the jar will provide access to its contents. If these documents are in anyway 'sensitive' I would suggest looking at encrypting them.

Cryptography itself is a broad subject that requires real understanding to create a secure solution, but the following has a basic example of encrypting and decrypting data using java.

http://www.software-architect.net/articles/using-strong-encryption-in-java/introduction.html

EDIT

The link suggests using the following:

The same code to generate the 'aesKey':

 String key = "Bar12345Bar12345"; // 128 bit key
 // Create key and cipher
 Key aesKey = new SecretKeySpec(key.getBytes(), "AES");
 Cipher cipher = Cipher.getInstance("AES");

To encrypt a string:

    // encrypt the text
     String toEncrypt = "Your text.";
     cipher.init(Cipher.ENCRYPT_MODE, aesKey);
     byte[] encryptedBytes = cipher.doFinal(toEncrypt.getBytes());
     String encrypted = new String(encryptedBytes);

To decrypt a string:

     // decrypt the text
     cipher.init(Cipher.DECRYPT_MODE, aesKey);
     String decrypted = new String(cipher.doFinal(encrypted.getBytes()));

The stored text in the file will be completely unintelligible. There are other things to consider; for example if you store the key in a class it will be possible to extract it. But with all security measures, the complexity of creating it has to balanced against the difficulty involved in working against it. This level is ultimately dependant on the nature of the information you are securing.


If you simply want to package the text files inside the jar, and the secruity of them is not an issue, see the following (assuming you are using eclipse):

How to package resources in Jar properly

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM