how to import source code into Eclipse from a jar file? - java

I got a jar file that is packaged with source code in it. I want to load it as a top-level project and start working on the source code. How can I do that in Eclipse?
For example: if I have helloworld.jar, when I import this jar. I want to have the project name as helloworld, with all the packages and src inside it.
I do not want that code on build path.but on my IDE to work with.

Create a directory named "helloworld" and unpack the JAR into that directory:
mkdir helloworld
cd helloworld
jar xvf ../helloworld.jar
You can then create a project from existing sources and start operating on that. I don't think Eclipse will let you create a project and change files directly in the JAR.

#eagertoLearn Yes. Eclipse does this in a very easy way than suggest by #Erik Gillespie.
Use Import from archive file feature in eclipse. Refer this post https://stackoverflow.com/a/11983201/1391924
Supported archive file types:
*.jar
*.zip
*.tar
*.tar.gz
*tgz

Right click on the .jar and choose open with -> winrar (or another archiver) and then click extract.
From there you can import the folder in Eclipse as a new project.

I was able to import the jar file contents using the following steps:
Create a new java project
Import jar file using file>import>general>archive file option
Move the folder containing source code into the src folder using drag and drop
Run the class containing the main method

Related

How to Import an External .class File in an Eclipse Java Project

I am trying to include a .class file through an import in a .java class I am creating. I need the .java class to look the same as what I need to deploy on the application server. I have created a directory structure to put the Context.class file in.
I updated the build path by using "Add External Class Folder..." and selected the "oracle" directory containing apps>fnd>common.
However, Eclipse still can't find the Context.class file referenced in the import.
What am I doing wrong?
You need to select the classes (C:\Users\badams\java\classes) directory instead of the oracle directory when using Add External Class Folder....
You should provide the root of the directory that represent your java package structure.
As an alternative, you can create a jar archive with the contents of the C:\Users\badams\java\classes directory and add that archive as a jar dependency as well.
You can use for this purpose the builtin SDK jar utility. Please, change you current directory to C:\Users\badams\java\classes and run from a command prompt or PowerShell terminal something like:
jar cvf oracle-fnd.jar *
Then add the generated archive to your project as usual.

Can't use class from jar in netbeans

libraries properties
For one of my java courses, I need to import a pre-compiled java class in netbeans. I can't use it apparently.
So my prof is using grasp, which I don't like as IDE so I'm using netbeans. She is useless for my problem, so the problem here is that she has compiled the Joueur.class from java libraries and she wanted that we use it in our project.
So I managed to transform the class in a jar file with jar.exe -cf, then I added it with right-clicking on libraries in my project in netbeans.
Netbeans listed i,so it apparently recognized it. But when I try to use methods from it, it's underlined. But I've read that netbeans will recognized the contents of the jar automatically so I should be able to use it without a problem.
Is there an import to do in the main package or something to use it? I can't find the information ...
global netbeans
The name of the package for the class has to match the directory path in the jar. So if you want to create the jar correctly for class x.y.MyClass, you need to create the directories for it, copy the class file to it and then use jar to create the jar file. Here are some example commands:
mkdir x
mkdir x\y
copy MyClass.class x\y
jar -cf MyJar.jar x
To verify, type the following:
jar -tf MyJar.jar
It should print out something like:
META-INF/
META-INF/MANIFEST.MF
x/y/
x/y/MyClass.class

Create project from existing project Netbeans

I have downloaded some source code that contains src folders with some .java files. I want to try to run the code in netbeans.
This is the structure of the folder :
C4.5/src/main/java/myc45/
and in these folder include some .java files.
What should I do first?
When you create a project in Netbeans, one of the options in the project creation window is create project from existing source. If you have an existing project, you can also edit the project properties and tell it what the source folders are.
As an alternative to #PaulJAbernathy 's solution:
create a new project in Netbeans
via the projects windows, create a package myc45 (the package name used in the code you want to import) - you can do so by rightclicking, new Package
now, inside the src directory of your project directory you'll find a directory called myc45. Drop the source files into that directory using whatever file browser you commonly use. You'll see that Netbeans picks up the files almost immediately in the project explorer.
you can now use the code.
A bit messy, but there are advantages: if eg you want to transform a bunch of existing code files into a Maven type project, this is probably the easiest way.

Making a JAR file which includes all files of a cetain type from any subdirectory

So this is a simplified version of my package structure
Project 1
-folder1
-folder2
-folder21
-folder211
-test3.java
-folder22
-folder3
-test4.java
-Project2
-folder1
-folder11
-folder111
-Test.java
-folder2
-.properties
-Test2.java
-folder3
What I want to find is command that will create a jar and take the paths to project1 and project2 and recursively add the folder structure and java files without adding the .properties files.
What i've tried so far is
jar cvf test.jar "pathtoproject1/.java" "pathtoproject2/.java"
That only works for java files in the base project directories not the subfolders.
Anyone know how to do this?
edit
This is for a batch script on windows
Frankly I'm not sure that jar handles this out of the box.
I suggest using ant - with an ant jar task, using a fileset.

Updating .class file in jar

I want to update a .class file in a jar with a new one. What is the easiest way to do it, especially in the Eclipse IDE?
This tutorial details how to update a jar file
jar -uf jar-file <optional_folder_structure>/input-file(s)
where 'u' means update.
Do you want to do it automatically or manually? If manually, a JAR file is really just a ZIP file, so you should be able to open it with any ZIP reader. (You may need to change the extension first.) If you want to update the JAR file automatically via Eclipse, you may want to look into Ant support in Eclipse and look at the zip task.
Use jar -xvf to extract the files to a directory.
Make your changes and replace the classes.
Use jar -cvf to create a new jar file.
Simply drag and drop your new class file to the JAR using 7-Zip or Winzip. You can even modify a JAR file that is included in a WAR file using the parent folder icon, and click Ok when 7zip detects that the inside file has been modified
Jar is an archive, you can replace a file in it by yourself in your favourite file manager (Total Commander for example).
A JAR file is just a .zip in disguise. The zipped folder contains .class files.
If you're on macOS:
Rename the file to possess the '.zip' extension. e.g. myJar.jar -> myJar.zip.
Decompress the '.zip' (double click on it). A new folder called 'myJar' will appear
Find and replace the .class file with your new .class file.
Select all the contents of the folder 'myJar' and choose 'Compress x items'. DO NOT ZIP THE FOLDER ITSELF, ONLY ITS CONTENTS
Miscellaneous - Compiling a single .class file, with reference to a original jar, on macOS
Make a file myClass.java, containing your code.
Open terminal from Spotlight.
javac -classpath originalJar.jar myClass.java This will create your compiled class called myClass.class.
From here, follow the steps above. You can also use Eclipse to compile it, simply reference the original jar by right clicking on the project, 'Build Path' -> 'Add External Archives'. From here you should be able to compile it as a jar, and use the zip technique above to retrieve the class from the jar.
Editing properties/my_app.properties file inside jar:
"zip -u /var/opt/my-jar-with-dependencies.jar properties/my_app.properties". Basically "zip -u <source> <dest>", where dest is relative to the jar extract folder.
High-level steps:
Setup the environment
Use JD-GUI to peek into the JAR file
Unpack the JAR file
Modify the .class file with a Java Bytecode Editor
Update the modified classes into existing JAR file
Verify it with JD-GUI
Refer below link for detailed steps and methods to do it,
https://www.talksinfo.com/how-to-edit-class-file-from-a-jar/
1) you can extract the file into a folder called
jarname.jar
and then replace the file in the folder, handy if you are updating the class a lot while debugging
2) you can extract the jar replace the file then the jar it up again
3) Open the jar with 7 zip and drag and drop your new class in to copy over the old one
You can find source code of any .jar file online, import the same project in your IDE with basic setups. Make necessary changes in .java file and compile it for .class files.
Once compilation is done You need to extract the jar file, replace the old .class file with new one.
And use below command for reconstruct .jar file
Jar cf test.jar *
Note : I have done so many time this changes in our project, hope you will find it useful.
An alternative is not to replace the .class file in the jar file. Instead put it into a new jar file and ensure that it appears earlier on your classpath than the original jar file.
Not sure I would recommend this for production software but for development it is quick and easy.

Categories