[英]how to create a jar with compiled classes from a source jar in java
I know how to create a jar from .java or .class files.我知道如何从 .java 或 .class 文件创建一个 jar。
I have a sources.jar
that contains .java
files.我有一个包含
.java
文件的sources.jar
。 I want to compile these classes and pack into a jar
again.我想编译这些类并再次打包到
jar
。
for example: This is the jar I want to compile:例如:这是我要编译的 jar:
hadoop-mapreduce-examples-2.2.0-test-sources.jar
. hadoop-mapreduce-examples-2.2.0-test-sources.jar
。
I did jar xvf hadoop-mapreduce-examples-2.2.0-test-sources.jar
to extract everything.我做了
jar xvf hadoop-mapreduce-examples-2.2.0-test-sources.jar
来提取所有内容。
I now have我现在有
ls
META-INF org
ls org/apache/hadoop/
examples mapreduce
ls org/apache/hadoop/examples/
TestBaileyBorweinPlouffe.java TestWordStats.java pi terasort
As you can notice, I have two packages within org.apache.hadoop
, "examples and mapreduce", both of which have to be compiled.如您所见,我在
org.apache.hadoop
有两个包,“examples 和 mapreduce”,这两个包都必须编译。 And I have sub-packages within "examples" that needs to be compiled.我在“示例”中有需要编译的子包。
javac org/apache/hadoop/mapreduce/lib/db/TestDBJob.java
This compiled fine这编译得很好
But How can I compile recursively.但是我如何递归编译。 I tried using wildcard '*'.
我尝试使用通配符“*”。
This is what I tried:这是我尝试过的:
javac -cp org/apache/hadoop/examples/*
javac: invalid flag: org/apache/hadoop/examples/pi
Usage: javac <options> <source files>
use -help for a list of possible options
javac org/apache/hadoop/examples/*.*
In the last javac
executed above, I don't see the subpackages being compiled.在上面执行的最后一个
javac
,我没有看到正在编译的子包。 only the top-level java files was compiled.只编译顶级java文件。
Is there a simple solution such as是否有一个简单的解决方案,例如
javac -jar <input.jar> <output.jar>
will result in <output-jar>
that has compiled files in it. javac -jar <input.jar> <output.jar>
将导致<output-jar>
已在其中编译文件。
EDIT :编辑:
To re-iterate my question:重申我的问题:
given a jar that has only .java
files, I want a jar file that has .class
files in the simplest possible way给定一个只有
.java
文件的 jar,我想要一个以最简单的方式包含.class
文件的 jar 文件
From the DOS console从 DOS 控制台
cd /dir/folder of .class files. .class 文件的 cd /dir/文件夹。
jar cvf name.jar jar cvf 名称.jar
Here's a really dirty script I wrote to build compiled JAR out of a sources JAR.这是我编写的一个非常脏的脚本,用于从源 JAR 构建已编译的 JAR。 It probably won't work for everything, and doesn't even handle copying the Manifest:
它可能不适用于所有情况,甚至不处理复制清单:
#!/bin/bash
if [ "$#" -lt 1 ]; then
echo "Usage: $0 sources.jar [classpath]"
echo
exit 1
fi
OUT_JAR=`basename $1-compiled.jar`
rm -rf $OUT_JAR
rm -rf src
rm -rf classes
mkdir src
mkdir classes
if [ -n "$2" ]; then
CLASSPATH="-cp $2"
fi
unzip -d src $1
if [ $? -ne 0 ]; then
echo "Unzip failed"
exit 1
fi
find src -name *.java | xargs javac -d classes $CLASSPATH
if [ $? -ne 0 ]; then
echo "Java compilation failed"
exit 1
fi
jar cf $OUT_JAR -C classes .
if [ $? -ne 0 ]; then
echo "Jar creation failed"
exit 1
fi
echo
echo "Compiled and built $OUT_JAR"
Also available here: https://github.com/satur9nine/sources-jar-compiler/blob/master/compile-sources-jar.sh也可以在这里找到: https : //github.com/satur9nine/sources-jar-compiler/blob/master/compile-sources-jar.sh
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.