简体   繁体   中英

Store text files safely in JAR

Is there any way in Java to store external files (inside jar) safely? Java files are compiled to .class files and not readable.

I'm planning to use this meganism to store sql files, which hold queries, however i dont want those to be readable when one extracts the jar. External files are pre, cause we then have syntax highlighting while developping instead of putting them into a string.

A jar file is pretty much just a .zip file, so you can put whatever you want in them. There's no built-in encryption, so if you're trying to make the SQL files non-readable, you'll have to encrypt them (and then unencrypt in your code). I don't understand what you mean by "External files are pre, cause we then have syntax highlighting while developping instead of putting them into a string."

Yes, people do this all the time. Just add the files to the jar, and then access them with

getResourceAsStream . If you build with maven, you just plop them into src/main/resources to get them into the jar.

Of course you can put them in the Jar. Once there you can get at them by using the ClassLoader's getResourceAsStream method.

The challenge is to prevent other people extracting them. If your code can read them, so can someone else. What are your requirements here?

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