Using JAR resource as File in java or scala - java

I have developed an application that basically searches its resources/runtime directory, looking for .java files.
Having fetched all .java sources, it (together with program arguments) passes their absolute paths to JavaCompiler.
Of course, this approach doesn't work when application is deployed as a JAR package. So the question - how can I work with resource files as with regular files, i.e. pass their absolute path and read them afterwards?
I found some related topics here:
reading the file as a stream
making a resource copy to some temporary file
But what if I don't know names of the files? I seek solution that allows me to search the resources/runtime directory. Moreover, I think making a copy of file somewhere and delete it later on is clumsy solution.
I use combination of Java and Scala (with sbt build system), hence solution Java or Scala would be great.

Related

How do I organize and access external files nicely in IntelliJ?

I'm programming using IntelliJ IDEA. I'm programming a game. This game needs to save its data inside some files (for example player data, world map exploration data etc.). The problem arises when I want to separate my development environment from the exported .jar executable. When referring to a file in my project, I use a path like "saves/world1/players/player1.data". This file is accessible by using File("saves/world1/players/player1.data"). However, the more files I need, the more my project gets cluttered, because all the files are added into my project root directory. Also, when exporting the .jar artifact, it only exports the .jar file and I have to copy all the other files manually into the same directory as the .jar file. How do I automate this process and how do I organize the files a little better (like putting them in one directory to not clutter my project root folder)? Also, I should mention that I use Kotlin, but I don't this it's important for this question. A Java solution might work just as well.
Have you application declare a file path property from where you load these large external data files. For local dev/testing this can default to a path local to your project but for deployment you'd perhaps have start or execution scripts which allow the user to configure where there '.data' file located.

How to obfuscate names of the resource files located in a Java JAR archive?

I've created the JAVA desktop application which uses a large number of files located in a resource folder inside the JAR file. I would like to prevent the user of my app from uncovering the names of these files (someone could achieve that simply by extracting the JAR archive).
Is there any way to obfuscate or rename in bulk these filenames both in the JAVA code and in the resource folder? I tried to find this kind of solution in ProGuard, but I couldn't.
Of course, I could manually change the names of the files and the code lines to replace the original ones, but there are over thousand of them and it would take ages. I would also prefer rather to obfuscate than rename the filenames, because keeping the original filenames in my code would make my work easier. Do you have any ideas what could I do?
Thank you for your support.
Bartek

Using Java Web Start when main jar depends on other files

I have recently completed a java program and Im now looking into options for deploying it. I was introduced today to Java Web Start. My problem is the following: my program contains the main executable jar, a lib folder that contains the libraries which my program uses, and a couple of other folders which contain files that my program reads from and writes to. Is it possible to use Java Web Start with such a structure? All the examples I have seen on the web were simple programs made up of just one jar. Thanks.
The extra libraries are no problem. Just add a reference to each one in the JNLP resources section.
The 'loose files' are, slightly, in that loose resources cannot be referenced in a JNLP and archives are not writable. But even that is fixable. See the accepted answer to How can an app use files inside the JAR for read and write?

Java Game refering to files

I have started getting into game programming.
My question is, that when I am working with files, either parsing data, writing to files, etc. Should I be using relative path names, or absolute pathnames, or something else which is better. I've heard about using jar files, but I am not sure
1. how that works
2. if it is a good way to do it.
So when developing a game that will be cross platform, what is the best method for managing files that the program will need to read from and write to.
there are several ways in which you can ship your code as a product. the most common are
packaging everything in one executable jar file.
having a set of folders where you place all necessary resources.
minecraft, for example, is written in java and distributed as a single executable jar file that contains all necessary class files and resources. to run the game (assuming you have java installed) all you need to do is double-click the jar file.
read this short tutorial about how to add a main class to a jar file.
either way, always treat classes and resources in your code as if they're in your classpath. for example, if you have a my.properties file on the root of the source tree then load it by using 'my.properties'. if you put it under a 'conf' folder then use 'conf/my.properties'.
i think it is the safest way not to get lost.
are you using maven?
The jar file is a zip of all your compiled *.class files and your resources. You can safely load your resources and even default data FROM a jar if you package your program, but you can NOT safely write data back to the jar. This detail is answered in depth already at
How can an app use files inside the JAR for read and write?
For information on how to package a jar see
http://docs.oracle.com/javase/tutorial/deployment/jar/

Problems importing WAR files in Eclipse?

I was unfortunately forced to result to uploading a WAR file as my backup for a web application I am working on.
Luckily I have the most recent WAR file available. I am using Eclipse IDE and am using the Web Tools plugin for all the J2EE work that I am doing with the Dynamic Web Application Project.
When I imported my WAR file, and ran it on a local server, everything works fine. The problem I a ran into is that in the Java Resources/src folder that all my packages and .java files were now only consists of all the same packages, but they are empty.
I checked to see if I could find the files and I found the .class files in an "Imported files" folder that is not accessible in the Eclipse Project Explorer. I believe that I need to do some type of build or something so that my .java files are available for me, but unfortunately this is one area where I lack.
One thing I would also like to know is, one way or the other, am I able to obtain the .java source code files if I have access to the .class files?
Also, I would like to configure this environment as it was before where my Java Resources:src folder contaiend the packages and .java files.
One thing I would also like to know is, one way or the other, am I able to obtain the .java source code files if I have access to the .class files?
The short answer is No. There is no way to regenerate original source files from bytecode files.
If you were really, really desperate you could try to use a Java bytecode decompiler on your bytecode files, but the result will be be nothing like your original source code.
All comments and javadocs will be gone.
All original code layout will be gone.
Original local variable and parameter names may be gone, depending on your original compiler switches.
Constant expressions may have been pre-evaluated, and loops, string concatenations and other constructs may have been transformed unrecognizably.
Depending on the maturity of the decompiler, the Java code might not be semantically equivalent to the original code, and might not even be compilable.
I hope you haven't spent too long developing this application because the best answer may be to start again.

Categories