问题
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
回答1:
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.
回答2:
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.
回答3:
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.
回答4:
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
回答5:
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.
来源:https://stackoverflow.com/questions/59488252/how-to-edit-java-files-imported-in-eclipse-from-a-jar-file