Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I prevent javac accessing the Class-Path from the manifests of our third party jars?

Since Java 1.5 or so, javac has been looking into the manifest of third-party jars to find other jars. This causes a number of undesirable side-effects:

  1. As jar files have been renamed, we now get a flood of warnings whenever we compile (can be diabled with -Xlint:-path)
  2. Files we don't want on the classpath are being brought back onto it, even if they were left off it for a reason.
  3. Additional time is being taken in the build to look up all these additional jars, due to the resolution of this stuff we don't actually want.

So I was wondering if anyone knows the magic invocation to disable this. Assuming that Sun didn't saddle us with another feature we didn't want and can't turn off once we have it.

like image 747
Hakanai Avatar asked Sep 27 '10 00:09

Hakanai


People also ask

What is classpath in Javac?

CLASSPATH is an environment variable which tells javac or java where to find the user defined classes it must refer to. CLASSPATH consists of a colon separated list of directories. The directory containing Java system classes will be automatically appended at the end of the list by the system.

How do I specify multiple JARs in classpath?

In general, to include all of the JARs in a given directory, you can use the wildcard * (not *. jar ). The wildcard only matches JARs, not class files; to get all classes in a directory, just end the classpath entry at the directory name.

How do I find the classpath of a JAR file?

To check our CLASSPATH on Windows we can open a command prompt and type echo %CLASSPATH%. To check it on a Mac you need to open a terminal and type echo $CLASSPATH.

What is RSRC class path?

In short, Rsrc-Class-Path is an attribute that the JarRsrcLoader class understands, and uses to construct the actual application classpath. In this context, the Class-Path: . attribute serves no real purpose. Everything needed to run JarRsrcLoader will be in the JAR.


2 Answers

Heres an Ant target to modify the manifest files(uses ant-contrib)

<target name="util-modify-manifest" depends="build-classpath">
<for param="file">
    <fileset dir="${jars}" >
        <include name="**/*.jar" />
    </fileset>
    <sequential>
        <jar jarfile="@{file}" destfile="@{file}" update="true">
            <manifest>
                <attribute name="Class-Path" value="" />
                <attribute name="Export-Package" value="" />
            </manifest>
        </jar>
        <echo message="Manifest Replaced: @{file}" />
    </sequential>
</for>

like image 74
Derek Avatar answered Sep 29 '22 21:09

Derek


Use bnd or shade to strip the offending MANIFEST.MF entry from the jars, instead of just renaming. Or take advantage of the face that these pathnames are essentially never absolute. If you move the jar named 'i-have-a-ClassPath.jar' into its own subdirectory, the manifest class path entries will fail to find these other jars in the expected locations. I suppose that will still whine if you turn on enough lint, though.

like image 27
bmargulies Avatar answered Sep 29 '22 20:09

bmargulies