开发者

prog.java and prog.class in Prog.jar file - How can I easily update this code?

Back story:

Long ago, before dinosaurs were around, there was a contractor that wrote a small Java program, that does some stuff, and prints some stuff to the screen.

Current: No one here knows much about Java but me - and I am not fluent.

The program works, but we kind of need just a bit more information on the screen. From the documentation available, I think I know how to go about that, and I definitely know how to print stuff, as it's simply using System.out.println() for this.

I have a .jar file, which I'm 99% certain is being loaded/used, which contains .java files matching every .class file within.

Is there an easy way to (and how might I) slightly modify one of the .java files, and 开发者_StackOverflow中文版"compile" the new version to replace the current matching .class file within the .jar?

I'm able to open and read the java source file, of course, but don't remember to procedures to turn java into "class" files, and especially not sure if I can just drop the resulting .class file into the .jar file as a replacement.


at the basic level, javac is the compiler; that will produce the .class files. There is also a "jar" command that will regenerate your jar file. Depending on the number of dependencies, that can get complicated quick. You can just type "javac" and "jar" (w/o quotes) to get the args to run it. Another option is to setup an ant build script...

so you could do something like (assuming windows OS):

javac -cp %MY_CLS_PTH% *.java
where MY_CLS_PTH is where any dependent classes are. If you have a package structure, this gets a little more complicated.

jar -cvf .\my_class_dir MyJarFile.jar where my_class_dir is the directory that contains the .class files.

I think that is right (didn't run it myself) but that should be about the basics...

EDIT: There is a way to just add/re-add a single .class file to an existing jar file. Check out the "jar" command's usage, looks like "-u" will update it (of course you have to pass in the .class file). Also remember that any package structure you have in these classes, you need that directory structure and you reference the class via that dir structure. (Hope that is clear...)


Add myclass.class to the top level directory in myjar.jar:

jar uf myjar.jar myclass.class


Add myclass.class to the subdir directory in myjar.jar:

jar uf myjar.jar -C subdir myclass.class


Add a Version attribute to the manifest of myjar.jar:

First create a file somefile containing the line Version: "X.Y.Z", then

jar umf somefile myjar.jar
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜