简体   繁体   English

将相关文本文件添加到JAR文件

[英]Adding dependent text files to JAR file

I have a JAR file and it depends on 5 different text files. 我有一个JAR文件,它取决于5个不同的文本文件。 The JAR file read the text files and give the result. JAR文件读取文本文件并给出结果。 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. 请建议我如何将这些文本文件添加到JAR包中。 If someone knows JAR-TO-EXE tool so that the text files are hided in-to the EXE then it is acceptable too. 如果有人知道JAR-TO-EXE工具,以便将文本文件隐藏在EXE中,那么也可以接受。

I suggest you look into Serialization as a possible solution, here's a link. 我建议您将序列化作为一种​​可能的解决方案,以下是链接。

http://www.tutorialspoint.com/java/java_serialization.htm 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). 我不是100%确定无论如何都无法重建数据,但是...打开文件时,文件的内容肯定会变得乱七八糟(即使您不想为此使用它,无论如何也要学习它非常有用) )。

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. 编辑:我想详细说明一下,以便您知道它的基本含义(如果您不知道)...想象您有一个包含对象(需要保存的数据,如您自己的员工类之类的对象)的arraylist。您基本上可以将该列表序列化为文件,并且想要加载数据时只需将其反序列化即可。保存数据的文件将对打开它的任何人来说都是乱码。实际上,这比处理普通文件要容易得多必须处理读取的每一行,等等。还记得您必须在需要使用它的类中实现Serializable接口。

Made you a simple example, below I'm using an extremely simple class called testing which only contains a name: 让您成为一个简单的示例,在下面,我使用了一个非常简单的类,称为test,仅包含名称:

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. 因此,您无法在主要方法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. 但是您将不得不继续阅读,尽管imo它将是一个非常优雅的解决方案。

You can 'hide' the files in the jar, but they will still be accessible. 您可以“隐藏” jar中的文件,但是仍然可以访问它们。 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. 密码术本身是一个广泛的主题,需要真正理解才能创建一个安全的解决方案,但是下面的示例提供了一个使用Java加密和解密数据的基本示例。

http://www.software-architect.net/articles/using-strong-encryption-in-java/introduction.html 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': 相同的代码生成“ 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): 如果您只想将文本文件打包在jar中,并且它们的安全性不是问题,请参阅以下内容(假设您使用的是eclipse):

How to package resources in Jar properly 如何在Jar中正确打包资源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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