How can I splice files (ANY files, pngs, classes you name it) and folders into a pre-existing jar file ONLY using java code. That means no jar.exe utility. I am planning to make a program that places files/folders into a specific jar file. I have looked at several tutorials on java.util.jar and other jar managing modules but none of them seem to be right for me. Do I have to do this from scratch? Here is an over view of what I am thinking:
Take the desired files and folders from within a specific folder
open the target jar and place the files and folders within, overwriting other files if need be
I'm thinking I have to decompile the jar and then repackage it (ONLY USING JAVA CODE). I don't know what to do.
Related
I have a question, perhaps it was already answered, but i didn't manage to find it and I appologize if the solution already exists (let me know if it is before deleting my thread).
Problem is:
I have created a program on another PC and exported it from eclipse as a .jar file. It works on my main PC when I double click on it but when I import it in Eclipse I can't find the .java file. So i can't edit it.
What I have done so far:
In eclipse I have created a new empty project
I have right clicked,import, archive file, selected the .class files that eclipse sees, but when I am in the Project Explorer in Eclipse I can't find the .java file where the main is. I mean I can click run as a program and it works, but there is no .java file, only .class files. What am I doing wrong?
That cranes.class should be cranes.java. At least on my other PC it is.
Program works fine, but I can't edit it on my main PC. What am I doing wrong?
Thanks and best regards
You need to select the Export Java source files and resources option while creating the jar file and then your Java files will be available on importing the project from the jar file.
This is similar to how you use other libraries. You depend on the Jar file which contains class bytecode (compiled) of java code. You can't edit any of such files directly in the project you are using it. Thought you can always extends functionalities in your current project using simple inheritance concepts.
If you think such functionalities are trivial you should prefer to change in the original project rebuild the jar and use the newer version of jar.
However if you feel similar things for 3rd party library you can
always make changes after taking fork from those library source
code (if open source) and build and use your own version or go
ahead and raise pull request if you are confident about your
changes.
Mostly when you build a jar file, all you have in it are .class files; these are the result of compiling .java files, and so are not editable with text editors.
You CAN create a jar file that contains .java (also known as source) files, and even a jar file that contains both .java and .class files, but if you ask eclipse to create a jar file, by default it is just going to put .class files and files from resource folders in it, not .java files.
Assuming from the question, the jar is a library created by OP, by compiling java files into class files and packing/exporting them. Although the class files can't be edited in any IDE, they can be de-compiled into Java files by using third-party applications.
I personally use IntelliJ for this de-compiling source files authored by me
Note: Although this gives OP the desired functionality, it may lead to violations if the classes are Copyrighted.
As IntelliJ states, they neither encourage nor discourage de-compiling class files and the decision is purely to the user's discretion.
EDIT: It is always recommended to use the original source files for editing. Try to host them on git so that it may be retrieved anytime required
It may be simpler to not use eclipse but jar/zip/tar your project directory on the one computer and simply extract it onto the other, then open that folder as a new project in Eclipse.
Best is the suggestion from #SanjayBharathi to use git and clone the repo on your other machine.
I have a Netbeans maven project, and am using the src/main/resources folder to deploy my pictures and a help file alongside the program that I'm writing.
From my understanding of the subject, any files in this folder (and the packages that contain them) are archived in the generated .jar file generated by the Netbeans IDE. I've verified this by opening the jar in a compressed file extraction program.
This is convenient for deployment, and previously, I ran into issues with versioning, with some pictures being out of date, as I was using folders on the disk to house the pictures folder previously.
I have a subroutine that selects a random picture from the pictures folder, but implementing this inside the jar file clashes with my understanding of the subject.
If the pictures folder is now in the jar archive, how can I get Path objects to these files? I'm well aware that I can use Clazz.class.getResource("/pictures") to get a URL object to the folder. I can also transform this URL into a URI and feed it to the File constructor and use getPath to get a path to this folder on the disk.
Then I can call Files.list(picturesPath); to get the list of Paths and select a random one.
This process would work just fine if the files/folders were just regular files/folders on the disk, but they're not, they're inside a compressed jar archive. This confuses me.
Can I just treat these folders I get back as Paths as typical folders and manipulate them in the typical way I do regular folders? Is there techno-sorcery at work to make this seamless or is there some subtle fallacy in my assumptions that would prevent me from using something like Files.list(picturesPath)?
So I've created just a simple application which I'm using to apply for a highschool internship. It was built using Eclipse.
I initially exported into a runnable .jar file, but the location I initially saved files, ("src/fileDirectories") didn't work on export.I then set the location to "./fileDirectories") which works and the app can actually run, however the .jar file creates the folder directory in the same folder as the .jar file.
This isn't too bad, as I can create a folder with the .jar and the saved files in it, which is fine, but similar to images, I'm wondering if there is a way to save .txt files in a directory to the .jar file so everything works with just the .jar application.
Assuming the files you want to access are static, and are not changed by the application, you can do this using the classpath.
When you access a file using new File("path"), Java will look for it in the file system, relative to the working directory (i.e. where the application was launched from.
An alternative way to access files is to look them up from the classpath - this is the list of locations Java searches for resources. It includes, among other things, the top level of your JAR archive. You can access this as follows:
this.getClass().getResourceAsStream("/my_file.txt")
Build tools will generally have preconfigured directories (e.g. src/main/resources) which get copied from your source tree into the JAR so they can be accessed in this way.
This is great if you have some static resources or config which you want to access at runtime, but don't want to have to pass around with your application.
However, if what you want is a working folder for files which your application will make changes to or create new instances of, like user documents, you don't want this to be editing the JAR - you'll need to make a separate folder for these.
I will explain what I need to do, in fact I found this answer Restoring code from JAR which is pretty close to my needs but with a key difference.
What I have is a folder with many jar files which include the .class but also the .java files of an application, I would like to restore the project into any IDE such as Eclipse or netbeans.
In order to make it clearer, the structure of what I have is the following:
Main_directory
File_1.jar
file_1_1.java
file_1_1.class
file_1_2.java
file_1_2.class
File_2.jar
file_2_1.java
file_2_1.class
file_2_2.java
file_2_2.class
I would really appreciate any suggestions but I would like to avoid using decompilers.
Best.-
.jar files can be opened and files extracted with many programs, such as tar, winrar, etc. Your best bet, considering these .jar files contain the actual .java files is to extract them with such a tool, create a new project in your ide(eclipse) and then import the .java files to the new project.
This one I couldn't find a proper answer.
I have 2 folders. One is called 'src', where
my java source code is located. The other one
is called 'srcGenerated' and has a set of files
created by a code generator. srcGenerated is a
superset of src.
I want to use both folders as my build path on
Eclipse. The problem is the duplicated files in
srcGenerated. Since there is no way to supress
the generation of files that are already in src,
my question is, how to delete the duplicated files
in srcGenerated based on the existing set of files
from src.
Ant or Powershell script preferred.
May not be the easiest one, but this is what I though of.
You can exclude Java files from srcGenerated that you don't want to be included while building.
Here you can exclude all the files that you don't want to be built. I know, it's painstaking if there are hundreds of conflicting Java files. But it definitely works.