Is it possible to move the .class files in a jar from the default package
to another named package so that I can import and use them?
After browsing other questions here, I think jarjar might be exactly what im looking for,
but I don't have any idea how I can use it.
So my question is how to use jarjar to repackage .class files from the default package to another named package?
EDIT: I want to clarify something - I do not have the sources of the jar I want to repackage,
so I don't know how I could recompile the code.
Obfuscation and reflection is out of the question.
EDIT2: It appears its extremely simple to do using jarjar :)
java -jar jarjar.jar process rules.rules in.jar out.jar
rules.rules look something like this:
rule a net.ylivay.src.a
Problem solved!
To move classes into a different package, you not only need to change their location in the jar (or filesystem) directory structure, you also need to change the package declaration in the source files and then recompile the classes.
Instead of trying to repackage a jar, you should probably look at the powerful refactoring tools available in IDEs like Eclipse, which make this kind of thing simple. But since you say (in update to your question) that you don't have the source files, that is not possible.
The alternative would be to create proxy classes which invoke the ones you need in the default package using reflection. But not only is that unpleasant, but you say that reflection is out of the question.
So you cannot do what you want to.
Related
I need to edit a single class file from within a jar.
I have successfully extracted the class file from the jar, and I have decompiled it and found the logic I need to change.
However, I'm unable to recompile this class file, because it imports libraries I don't have and don't know where to get (netbeans and iharder).
The needed files should all be within the jar, right? Can I use the jar for this purpose?
I do not understand much of Java's overarching syntax, so anything related to packages or jar file structure might go over my head...
The JVM just needs to be able to find the dependencies at runtime. Often, they'll be installed in a standard location (the classpath), rather than being bundled with the jar that uses them. However, you could theoretically even do something like download dependencies at runtime and load them via a classloader.
Apart from that, decompiling and recompiling is often not a good idea, since decompilation is a lossy and error prone process. It generally only works in simple cases, and has limitations, as you've discovered.
If you understand Java bytecode, you can edit the class by disassembling it with Krakatau, editing the .j file, and then reassembling. This allows you to edit any classfile without needing to compile, meaning you don't need the dependencies. It also works no matter how complicated the class is, and even works on obfuscated classes.
I read and searched almost everywhere, yet i have no answer.
I have a project with multiple .java files, and i need to export them as a single .jar file.
One of the main questions is based on the fact that i don't have a main class.
What can i do to go through that ? Shall i create a main but never run it? Or there's any way around?
Offtopic ( might be usefull): i want to do this in order to use the jar file in IKVM
Thank you!
You do not need a main method to create a jar. There are many such jars out there such as utilities packages. They don't contain any code that would make sense to execute on it's own since it only contains code that makes your application easier to write.
First compile your code using Netbeans or command line javac utility. This will give you .class files.
Now for creating the jar... You can simply create the jar (which is basically a glorified zip archive) by using the command-line jar tool.
jar -cvf my archive.jar myprogram/
Make sure that the file path within the jar archive match the package name of the classes. It is not uncommon to accidentally get an extra layer of directories.
You probably should have searched stackoverflow first since how-to-create-a-jar-file-in-netbeans seems to cover exactly what you're looking for.
Do you want to use the jar as a external library or a program itself?
If it will be a external library, you don't need a main class. In eclipse you can export as a jar file (select classes-> right click-> export as jar).
If it will be a program itself, then you need a main class.
You really need to create main class file, write code in function main(String args[]) which use your java classes and compile it by javac.
I'm trying to use a .class file I downloaded for a uni project. I don't have the .java file to go with it. I am using netbeans.
I tried just adding it to the project src folder
I tried using "add JAR/folder" on libraries and adding the directory containing it
I also tried creating a JAR file of the directory containing it and adding that
Greatly appreciate any suggestions
You should do two things:
Create a jar of the class file you received
Create an overview which methods the classfile offers
For the latter you have at least two options. One is to use a decompiler (some authors of APIs deny you to use this) like JD-GUI. The second options is to use javap, which comes with your JDK (I link to Java 8, but it exists in prior versions too). Simply call javap yourfile.class and you will see which method signatures the class offers.
But the easiest way to see the classes / methods inside the .class file is JD-GUI, so if you are not running into any legal issues use that approach.
I've always been impressed by the StackOverflow hive mind, and was hoping you could point me in the right direction here.
I've taken some courses in Java programming, and understand how to write a fairly complex Java program. However, I've never learned how to integrate others' software into my own programs.
For a new project, I'd like to integrate a part-pf-speech tagger and chunker into my code, but have no idea how to "load" these programs (if load is the correct term).
I'm certainly not looking for step-by-step instructions, but rather a guide for how to go about this sort of issue. If anyone could get me started in the right direction, I would greatly appreciate it.
Thanks,
Adam
It looks like the externals you want to use are themselves in Java. This means you're in luck - you can use pure java language features to make it work.
There are two things to it:
1) your source files that interact directly with the external libraries have to be imported, or otherwise you'll have to refer to them using the fully qualified classname.
Importing is done with the import statement. These statements should appear right before your class declaration, like so:
import foo.*; //import all classes from the package foo
import foo.bar.Baz; //import only the Baz class from the package foo.bar
public class MyClass {
Baz myBaz = null; //declare a member of type Baz class from package foo.bar
foo.bar.BazBaz myBazBaz = null; //by using a fully qualified classname, I didn't need to write an import statement for foo.bar.BazBaz
}
2) when you compile your sources, the java compiler needs to know where to look for classes you referenced in your source. This is done via the classpath.
The classpath can be a list of just .class files (compiled java classes), but also .jar files (java archives) and .zip files. Typically a project will package all classes it needs in one or more .jar files.
The location of these classes have no bearing on the way you interact with them in java code. It's the compiler's job to read these jars and class files and locate the classes you referred to in your code. If the compiler can't locate the classes you're referring to, you will get a compile time error and you can't compile your program.
You can specify the classpath as an argument to the java compiler command line (http://docs.oracle.com/javase/6/docs/technotes/tools/windows/javac.html#options). However, this becomes unwieldy very rapidly.
Instead, you should use a build tool like ant to do this work for you. The best way to get started is to read this page: http://ant.apache.org/manual/index.html.
From there, go to "Using apache ant" and then to "Writing a simple build file" in its entirety, they explain how to set up the classpath very well there.
You'll need their classes on your classpath when compiling, and again when running your program.
It appears that those projects distribute both src and jars. Grab the jars, and make them available on your classpath. Once your classpath is set up you'll need to import any specific classes/packages that you're using.
See this tutorial on managing the classpath. It covers the basics for command-line compilation/execution; if you're using a particular build system or IDE then the instructions will vary.
Also note the specific instructions at the second link for making data files available. For this they're also using the classpath.
Configure your build path to include the .jar files.
If you're using Eclipse, you would right click on the project file in the Project Explorer, then select Configure Build Path. Finally, add external archive (the ones you've downloaded). Now those functions will be readily available in your program.
Or a more robust way is to create a folder called "lib" in your eclipse project and include all jar files in there. Then from the Configure Build Path window you select those jar files in the lib folder. This makes it easy to share projects with other programmers regardless if they are on Windows or Linux (when adding external jar it saves the absolute path so if something on C:\ will not be found on someone else's PC) Also provides nice integration of dependency libraries on source code managers like GIT, CVS and SVN.
Well typically Java libraries are distributed as a JAR file. Then in your program, you can simply import the new packages and use the provided API.
When you compile and run, you have to make sure that the libraries are included in your class path so they know where to look for the packages.
I understand in java that you are forced into a single file per class.
So if I have classes like:
/my_project/main.java
/my_project/classes/user.java
/my_project/classes/other.java
And my main.java references the user and other files, how would I compile this via the command line?
If I was to have external .jar's that I was referencing, and I placed them in a particular folder, how could I also include this in my compiling? (or is there a general place I can put them where they will be picked up automatically like how python does this)
to compile, you will need to specify each source file, from the my_project folder:
javac classes/user.java classes/other.java main.java
You can also specify jar files for your classpath with the -cp option:
javac -cp myjarfile.jar main.java
You may also need to fiddle with the -cp flag to make sure your classes folder is in the classpath.
First of all it's poor style to make Java classes starting with lowercase.
Only public classes need to be in their own file, but you can add as many package-private classes as you like to the same file (although this is seen as poor style).
That said, the easiest way would to compile your code would be:
javac /my_project/main.java /my_project/classes/user.java /my_project/classes/other.java
In any case, proper code layout should be that classes are in a directory structure matching their package.
EDIT: There is a fairly good explanation of conventions here http://www.particle.kth.se/~lindsey/JavaCourse/Book/Part1/Java/Chapter05/packagesImport.html
In addition to the above answer, you can use something like Apache Ant, for easier configuration of your build (if it gets complicated).
Look at the documentation for javac. You can pass multiple source files, or specify the source directory.