Change the package of a JAR from the default package - java

I've downloaded a JAR file from my teacher's website containing some classes in the default package, and I'm thus unable to access them from inside a defined package.
I have read that the preferable solution is to repackage the JAR, changing the package name. However I have no idea how to go at it. The solution probably involves using Ant or Jar Jar, but I've no experience with either tool. I would love if someone coould point me in the right direction.
Thanks.

You need to change the sources and recompile then to change the package - simply moving the class files inside the jar (or outside) does not help.
So ask your teacher to give you the sources (or to put the classes in a suitable package), or use a decompiler to do this yourself.

You can unjar/unzip them manually, create the package and jar them back using and IDE or from the command prompt like this. Also, take a look at the ANT documentation on Jar and Unjar which is quite comprehensive.

As #Piyush Instructed use the below command for creating a Jar file.
jar -cvf *.* Example.jar

If you are using eclipse, just unjar the source files into the source folder of a temporary project. Then, create a new project (the real project you will be working on), and under the java/src directory, create the package structure you want. Then it's just a simple matter of drag-n-dropping the source files from the temporary project into the correct packages in the real project. Eclipse will take care of changing the package declaration of each class for you.

Related

How to make Java package with classes

I would like to make a Java package in a JAR file with precompiled classes such that other Java projects can consume these. How do I do that? It seems to me that most guides I have found expects a Main class/method to be available, but I do not want this to be an application that runs by itself. Furthermore, the resources (various files) inside of my project should be put into the JAR, since my app depends on these. Is this possible? I am (by the way) using Gradle.
A claim has been made that this question is a duplicate of this: Java creating .jar file. However, this question assumes the existence of main methods, and it does not concern how to include resources.
You can create the jar from the command prompt.
Copy all the classes that you want to include into a folder.
Then open that folder in command prompt and issue this command.
jar cfv YourProjectName.jar *
And a JAR will be created in the same folder containing all the classes.
Another solution:
If you are using eclipse try:
Right Click on the Package -> Export -> java -> jar file
You could also select the Classes and right click on them instead of the Package.
Edit:
Refer to https://docs.oracle.com/javase/tutorial/deployment/jar/build.html for more details on this command.
you can put all your methods/functions in class file then export it to .jar
then add the jar to your project's build path. Now you should be able to call those functions from your current main java class.

The simplest way to create jar file?

I have a java project and i need to create jar file from my project . any body can tell me what is the simplest way to make this?
The basic format of the command for creating a JAR file is:
jar cf jar-file input-file(s)
The options and arguments used in this command are:
The c option indicates that you want to create a JAR file.
The f option indicates that you want the output to go to a file
rather than to stdout.
jar-file is the name that you want the resulting JAR file to have.
You can use any filename for a JAR file. By convention, JAR filenames
are given a .jar extension, though this is not required.
The input-file(s) argument is a space-separated list of one or more
files that you want to include in your JAR file. The input-file(s)
argument can contain the wildcard * symbol. If any of the
"input-files" are directories, the contents of those directories are
added to the JAR archive recursively.
The c and f options can appear in either order, but there must not be
any space between them.
From scratch.. with CMD
The command line is what almost every other application will use to build your JAR file. They just wrap it up a little nicer for you. In truth, it's very simple to do yourself. Obviously Java have explained this way in detail so there is no sense in me repeating it.
Note: You need to have your JDK/bin directoy appended onto your %PATH% system variable to be able to use this method.
Double Note: As pointed out in the comments, I'd suggest you keep trying this method until you understand it. It is very important that you get these things at a low level, so if something goes wrong with the IDE, you have a much better understanding of how to solve it.
Eclipse
Eclipse offers a nice interface for it. You can find a step by step tutorial here. The long and short of it is..
Right click on Project -> Export -> JAR File -> Select the Java files to include
When you've done this, hit finish and you're golden. The tutorial also adds some additional tips in to make it as seamless as possible.
IntelliJ IDEA
My personal favourite IDE. This question offers a nice explanation for how to export as a JAR file. Again, the long and short of it..
File -> Project Structure -> Artifacts
In there, you can then create a new artifact by clicking the + icon. This will give you an option for the file type, which is .JAR and which modules you want to include in your artifact. When you're done, you go to..
Build -> Build Artifacts
And it will create the JAR file from your project.
Using Maven
I've often found this to be a pretty awesome tool, and definitely one worth considering. In IntelliJ, by double clicking the install procedure in the life cycles..
This will create a new JAR file in your .target directory.
Note: Some IDEs (like IntelliJ) will hide the .target directory by default. Make sure you make it visible in the project settings.
A JAR file is nothing but a ZIP file with added meta-information for the Java Runtime Environment. So the easiest way is to actually zip your classes files including that META-INF folder by hand, then rename the file to JAR. How you get your classes files however is a different story.
The easiest practical way is to hit the build button in your IDE, which will then compile your code into class files, create an appropriate set of meta information and then conveniently zips the whole thing into a JAR file.
Asking for a maven solution, you would need to specify a single pom.xml in your project
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>you.specify.something.preferably.a.domain.name</groupId>
<artifactId>name-of-project</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
</project>
And with this you can do a
mvn package
To build the project. However, it relies on a lot of defaults, you would probably want to differ.
To build a jar file from a module
On the main menu, choose Build | Build Artifact.
From the drop-down list, select the desired artifact of the type JAR.
see this

Java Edit Jar File

I have a jar file which includes seviral classes. In that jar there is a Confirmation.class file which i want to edit. I decompiled that class by usin JAD. Then i edit it with notepad++ and saved as .java file.
Now how can i create my new jar file with other classes?
Other files format is .class but mine is .java, is it problem?
If it is , how can i compile my .java class ? (when i use command javac Confirmation.java it gives errors and want other classes)
Thanks..
Do you have dependencies to other jars?
If this is the case, you will have to put them on the compiler classpath in advance.
In any case, just decompile all the package (I use jd-gui), change the class, compile, open the original jar as a zip, put the new class on it, and you are done.
Another approach is to create dummy classes for the missing dependencies...
Personally, I really discourage this approach of "reverse engineering" working with an already compiled package, but I understand situations where you don't have access to the original source code, but you need to fix something urgently (ex: working in a company, where your code comes from an external provider on the other side of the planet...)
You may try to copy & paste the code in Java IDE such as Eclipse, NetBeans, etc and ask the IRC to compile for u. Have a nice day :)

Referencing wrong jar file in java build-path

In my project we are referencing lot of dependency .jar files.
/lib/xxx.jar
/lib/abc.jar
The xxx.jar file having some (com.search.hit) packages. The same packages are available in abc.jar file.But the problem comes into picture now, where accessing xxx.jar file it doesn't referencing their package(com.search.hit) instead it is referencing abc.jar package.
Could anyone tell how to redirect the flow?
The class from whichever jar comes first on the classpath is the one that is used. The other one might not even exist as far as the classloader is concerned. It's a good idea to avoid conflicts like this.
In Eclipse go to your project build path configuration and click on the Libraries tab.
Then remove the package that you don't want to be accessed from your list and add it again.
This will cause the package to be lower in the priority list and it'll check the other package before the one you just re-added.
This will create problems for you. My suggestion would be to create 2 extra classes for writing getter and setter wrappers for these jar files. Write 2 seperate classes, each one of them will reference just one of them and your project file will use these wrapper classes to invoke functions from these jars. It would be a lot easier that way.
You have to change the package name it can not be same file name with same package in single JVM. JVM will take load randomly one jar class using class loader and ignore the rest.

how to update a external jar with my java class file on eclipse

In my java project, i have added some external jars. I want to update a particular jar with my java class file. Please help me how to do this.
Regards
Rajasekaran.G
If the jar you want to add your class to is a 3r party library, I would recommend against doing what you described (it can lead to subtle surprises and unintended side effects).
If the jar you want to update is your own, I would mode the source for the class into the project that generates that jar and it should be added automatically during the generation of the jar.
If you really want to do it by hand, you can unjar the external jar, add your .class file to the resulting directory structure and jar it up again.
You need to update the jar independently try this,
jar -uvf <existingjar> <class file in appropriate folder structure as per package>
e.g.
jar -uvf current.jar NewClass.class
or if your class is in some package say com.my.util, place your class file in com/my/util/NewClass.class and execute the following from prompt
jar -uvf current.jar com
This will update the current.jar file.
If its an external jar, i wonder if you can even do this? If its your own jar, follow the option given by Attila
try to open your .jar file winrar and place your java class wherever you want on the jar.

Categories