How to change compile only one class of a java source code - java

I am using Apache POI library on Java. I want to add a method to XWPFSDTContent class. I have the source code but when I want to compiling after adding method I have to use "ant jar" command but it takes so much time and makes it harder to debug because it compiles the whole library. Is there a way to compile only one source file? I can't use javac command because of its dependency on the library.

Assuming you have the source code of Apache POI imported into a java project in some kind of IDE. Then you basically only need to edit your one class, afterwards the IDE should have generated the new .class file.
Take this class file (and any possibly existing anonymous inner class files) and copy it into your jar (replacing the old .class files). That should work assuming that you compiled the class file with the same java version as the rest of the classes inside the jar and your jar is not signed.

Related

Convert .java files to single .jar

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.

Using .class files in netbeans

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.

Java code reuse with pre compiled class including Main

I downloaded all the Apache POI downloadables recently, specifically poi-examples-3.11-20141221.jar wherein it includes pre-compiled examples like "How to Use".
The problem is I can't run the pre-compiled classes without Eclipse.
Specifics: poi-examples-3.11-20141221.jar
-> org.apache.poi.xssf.eventusermodel
-> XLSX2CSV.class
XLSX2CSV is already compiled with main() and I just want to simply run it without eclipse.
Links through other tutorials about JAVA Reference Class and Jar in Java will also be helpful.
Im new here so please be gentle.
Run it with the jar on the classpath, but not with the -jar option
For example, for the .xls to csv converter example XLS2CSVmra you'd do something like:
java -classpath poi-3.12-beta1.jar:poi-examples-3.12-beta1.jar org.apache.poi.hssf.eventusermodel.examples.XLS2CSVmra
Make sure that all the POI jars you need are on the classpath (.xlsx / XSSF needs more), along with any of their dependencies from the lib directory. See the POI Components Page for details of what jars you need for what

Eclipse access to workspace, compiler and export function from code

I have following tasks:
Automatically generate java source files in the current workspace.
Compile those files after generation.
Export every generated and compiled class with needed libraries to runnable JAR file.
I already installed Eclipse SDK and I suppose what I need is to make my main class inherit some class from SDK and maybe load some other classes. But i don't know what do I need exactly and where to look. I'd appreciate some clues.
I suggest you look at M2T-JET to generate not only the Java files, but the project, any necessary folders and any other resources you need. One of those resources would be a jardesc file which is used by the JDT to persist jar export options. You can play around with those options to define the jar and export, then generate the jardesc file along with the other generated resources.
M2T-JET can be invoked programmatically, so once that single invocation generates your entire project, your plugin can make the call to the JDT to export the jar using the jardesc file.

Call Scala code from Java?

I'm using Netbeans to write Scala and Java.
Netbeans generated a .jar file for the Scala project. I tried importing that file in the Java project. But I couldn't import any class from that .jar file into my Java project.
I also tried importing scala-library.jar to the java project, and could import classes from that jar.
I want to write my library in Scala, then expose a small interface that only involves Java stuff, then write a Java wrapper so that people can use it as a Java package. Is that possible? How do I do it?
Thank you very much.
There should be no problem in doing this.
Have you verified (e.g. using WinZip or the jar utility) that your .jar file actually contains the relevant .class files? (use jar tvf mylib.jar to check)
If you have verified that the correct .class files exist in your jar file, what is the runtime error you are seeing? Is it a NoClassDefFoundError? If so, which class cannot be found? How are you referring to the (Scala) class in your (Java) code?

Categories

Resources