本文整理汇总了Java中java.util.jar.Attributes.put方法的典型用法代码示例。如果您正苦于以下问题:Java Attributes.put方法的具体用法?Java Attributes.put怎么用?Java Attributes.put使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.jar.Attributes
的用法示例。
在下文中一共展示了Attributes.put方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testRequiredJDKVersionTooOld
import java.util.jar.Attributes; //导入方法依赖的package包/类
public void testRequiredJDKVersionTooOld() throws Exception {
Path dir = createTempDir();
List<Integer> current = JavaVersion.current().getVersion();
List<Integer> target = new ArrayList<>(current.size());
for (int i = 0; i < current.size(); i++) {
target.add(current.get(i) + 1);
}
JavaVersion targetVersion = JavaVersion.parse(Strings.collectionToDelimitedString(target, "."));
Manifest manifest = new Manifest();
Attributes attributes = manifest.getMainAttributes();
attributes.put(Attributes.Name.MANIFEST_VERSION, "1.0.0");
attributes.put(new Attributes.Name("X-Compile-Target-JDK"), targetVersion.toString());
URL[] jars = {makeJar(dir, "foo.jar", manifest, "Foo.class")};
try {
JarHell.checkJarHell(jars);
fail("did not get expected exception");
} catch (IllegalStateException e) {
assertTrue(e.getMessage().contains("requires Java " + targetVersion.toString()));
assertTrue(e.getMessage().contains("your system: " + JavaVersion.current().toString()));
}
}
示例2: testBadMainClass
import java.util.jar.Attributes; //导入方法依赖的package包/类
/**
* Test that a JAR file with a Main-Class attribute that is not a qualified
* type name.
*/
@Test(dataProvider = "badmainclass")
public void testBadMainClass(String mainClass, String ignore) throws IOException {
Manifest man = new Manifest();
Attributes attrs = man.getMainAttributes();
attrs.put(Attributes.Name.MANIFEST_VERSION, "1.0.0");
attrs.put(Attributes.Name.MAIN_CLASS, mainClass);
Path dir = Files.createTempDirectory(USER_DIR, "mods");
String entry = mainClass.replace('.', '/') + ".class";
createDummyJarFile(dir.resolve("m.jar"), man, entry);
// bad Main-Class value should be ignored
Optional<ModuleReference> omref = ModuleFinder.of(dir).find("m");
assertTrue(omref.isPresent());
ModuleDescriptor descriptor = omref.get().descriptor();
assertFalse(descriptor.mainClass().isPresent());
}
示例3: testWithAddExportsInManifest
import java.util.jar.Attributes; //导入方法依赖的package包/类
/**
* Specify Add-Exports in JAR file manifest
*/
public void testWithAddExportsInManifest() throws Exception {
Manifest man = new Manifest();
Attributes attrs = man.getMainAttributes();
attrs.put(Attributes.Name.MANIFEST_VERSION, "1.0");
attrs.put(Attributes.Name.MAIN_CLASS, "TryAccess");
attrs.put(new Attributes.Name("Add-Exports"), "java.base/sun.security.x509");
Path jarfile = Paths.get("x.jar");
Path classes = Paths.get(TEST_CLASSES);
JarUtils.createJarFile(jarfile, man, classes, Paths.get("TryAccess.class"));
run(jarfile, "reflectPublicMemberNonExportedPackage", successNoWarning());
run(jarfile, "setAccessibleNonPublicMemberExportedPackage", successWithWarning());
// attempt two illegal accesses, one allowed by Add-Exports
run(jarfile, "reflectPublicMemberNonExportedPackage,"
+ "setAccessibleNonPublicMemberExportedPackage",
successWithWarning());
}
示例4: testMissingMainClassPackage
import java.util.jar.Attributes; //导入方法依赖的package包/类
/**
* Test that a JAR file with a Main-Class attribute that is not in the module
*/
public void testMissingMainClassPackage() throws IOException {
Manifest man = new Manifest();
Attributes attrs = man.getMainAttributes();
attrs.put(Attributes.Name.MANIFEST_VERSION, "1.0.0");
attrs.put(Attributes.Name.MAIN_CLASS, "p.Main");
Path dir = Files.createTempDirectory(USER_DIR, "mods");
createDummyJarFile(dir.resolve("m.jar"), man);
// Main-Class should be ignored because package p is not in module
Optional<ModuleReference> omref = ModuleFinder.of(dir).find("m");
assertTrue(omref.isPresent());
ModuleDescriptor descriptor = omref.get().descriptor();
assertFalse(descriptor.mainClass().isPresent());
}
示例5: testWithAddOpensInManifest
import java.util.jar.Attributes; //导入方法依赖的package包/类
/**
* Specify Add-Opens in JAR file manifest
*/
public void testWithAddOpensInManifest() throws Exception {
Manifest man = new Manifest();
Attributes attrs = man.getMainAttributes();
attrs.put(Attributes.Name.MANIFEST_VERSION, "1.0");
attrs.put(Attributes.Name.MAIN_CLASS, "TryAccess");
attrs.put(new Attributes.Name("Add-Opens"), "java.base/java.lang");
Path jarfile = Paths.get("x.jar");
Path classes = Paths.get(TEST_CLASSES);
JarUtils.createJarFile(jarfile, man, classes, Paths.get("TryAccess.class"));
run(jarfile, "setAccessibleNonPublicMemberExportedPackage", successNoWarning());
run(jarfile, "reflectPublicMemberNonExportedPackage", successWithWarning());
// attempt two illegal accesses, one allowed by Add-Opens
run(jarfile, "reflectPublicMemberNonExportedPackage,"
+ "setAccessibleNonPublicMemberExportedPackage",
successWithWarning());
}
示例6: updateClasspath
import java.util.jar.Attributes; //导入方法依赖的package包/类
/** Remove the jar file to the classpath in the MANIFEST.MF file */
@Override
protected void updateClasspath(Manifest manifest) throws DeployException {
Attributes mainAttributes = manifest.getMainAttributes();
String classpath = null;
if (mainAttributes != null) {
classpath = mainAttributes.getValue(Attributes.Name.CLASS_PATH);
}
String newJar = getJarFileNameWithDotSlash();
String newClasspath = null;
if (classpath != null) {
newClasspath = StringUtils.replace(classpath, newJar, "");
mainAttributes.put(Attributes.Name.CLASS_PATH, newClasspath);
if (classpath.length() < newClasspath.length()) {
System.out.println("Removed " + newJar + " from classpath");
}
}
}
示例7: createAgent
import java.util.jar.Attributes; //导入方法依赖的package包/类
public static File createAgent(Class<?> agent, Class<?>... resources) throws IOException {
File jarFile = File.createTempFile("agent", ".jar");
jarFile.deleteOnExit();
Manifest manifest = new Manifest();
Attributes mainAttributes = manifest.getMainAttributes();
mainAttributes.put(Name.MANIFEST_VERSION, "1.0");
mainAttributes.put(new Name("Agent-Class"), agent.getName());
mainAttributes.put(new Name("Can-Retransform-Classes"), "true");
mainAttributes.put(new Name("Can-Redefine-Classes"), "true");
JarOutputStream jos = new JarOutputStream(new FileOutputStream(jarFile), manifest);
jos.putNextEntry(new JarEntry(agent.getName().replace('.', '/') + ".class"));
jos.write(getBytesFromStream(agent.getClassLoader().getResourceAsStream(unqualify(agent))));
jos.closeEntry();
for (Class<?> clazz : resources) {
String name = unqualify(clazz);
jos.putNextEntry(new JarEntry(name));
jos.write(getBytesFromStream(clazz.getClassLoader().getResourceAsStream(name)));
jos.closeEntry();
}
jos.close();
return jarFile;
}
示例8: testBadAgentClass
import java.util.jar.Attributes; //导入方法依赖的package包/类
/**
* Test that java -jar fails when the executable JAR has the
* Launcher-Agent-Class attribute but the class cannot be loaded.
*/
public void testBadAgentClass() throws Exception {
Manifest man = new Manifest();
Attributes attrs = man.getMainAttributes();
attrs.put(Attributes.Name.MANIFEST_VERSION, "1.0.0");
attrs.put(Attributes.Name.MAIN_CLASS, "Main");
// agent class does not exist
attrs.put(new Attributes.Name("Launcher-Agent-Class"), "BadAgent");
Path app = Paths.get("app.jar");
Path dir = Paths.get(System.getProperty("test.classes"));
JarUtils.createJarFile(app, man, dir, Paths.get("Main.class"));
// java -jar app.jar
int exitCode = exec(app).shouldContain("ClassNotFoundException").getExitValue();
assertNotEquals(exitCode, 0);
}
示例9: testNoAgentMain
import java.util.jar.Attributes; //导入方法依赖的package包/类
/**
* Test that java -jar fails when the executable JAR has the
* Launcher-Agent-Class attribute and the class does not define an
* agentmain method.
*/
public void testNoAgentMain() throws Exception {
// manifest for the executable JAR
Manifest man = new Manifest();
Attributes attrs = man.getMainAttributes();
attrs.put(Attributes.Name.MANIFEST_VERSION, "1.0.0");
attrs.put(Attributes.Name.MAIN_CLASS, "Main");
// the main class does not define the agentmain method
attrs.put(new Attributes.Name("Launcher-Agent-Class"), "Main");
Path app = Paths.get("app.jar");
Path dir = Paths.get(System.getProperty("test.classes"));
JarUtils.createJarFile(app, man, dir, Paths.get("Main.class"));
// java -jar app.jar
int exitCode = exec(app).shouldContain("NoSuchMethodException").getExitValue();
assertNotEquals(exitCode, 0);
}
示例10: createMultiReleaseJar
import java.util.jar.Attributes; //导入方法依赖的package包/类
private static File createMultiReleaseJar(
@NonNull final File loc,
final boolean hasMultiVersionAttr,
@NonNull final Collection<Pair<String,Collection<Integer>>> spec) throws IOException {
final Manifest mf = new Manifest();
final Attributes attrs = mf.getMainAttributes();
attrs.put(Attributes.Name.MANIFEST_VERSION, "1.0"); //NOI18N
if (hasMultiVersionAttr) {
attrs.putValue(
"Multi-Release", //NOI18N
Boolean.TRUE.toString());
}
try (JarOutputStream jar = new JarOutputStream(new FileOutputStream(loc), mf)) {
for (Pair<String,Collection<Integer>> p : spec) {
final String fqn = p.first();
final Collection<Integer> versions = p.second();
final String path = FileObjects.convertPackage2Folder(fqn) + ".class"; //NOI18N
final String name = FileObjects.getBaseName(fqn,'.'); //NOI18N
final Collection<String[]> prefixes = new ArrayList<>();
for (Integer version : versions) {
if (version == 0) {
prefixes.add(new String[]{"","Base"}); //NOI18N
} else {
prefixes.add(new String[]{"META-INF/versions/"+version, version.toString()}); //NOI18N
}
}
for (String[] prefix : prefixes) {
final String pathWithScope = prefix[0].isEmpty() ?
path :
String.format("%s/%s", prefix[0], path); //NOI18N
jar.putNextEntry(new ZipEntry(pathWithScope));
jar.write(String.format("%s %s", name, prefix[1]).getBytes(Charset.forName("UTF-8"))); //NOI18N
jar.closeEntry();
}
}
}
return loc;
}
示例11: testAutomaticModuleNameAttribute
import java.util.jar.Attributes; //导入方法依赖的package包/类
/**
* Test JAR files with the Automatic-Module-Name attribute
*/
@Test(dataProvider = "modulenames")
public void testAutomaticModuleNameAttribute(String name, String vs)
throws IOException
{
Manifest man = new Manifest();
Attributes attrs = man.getMainAttributes();
attrs.put(Attributes.Name.MANIFEST_VERSION, "1.0.0");
attrs.put(new Attributes.Name("Automatic-Module-Name"), name);
Path dir = Files.createTempDirectory(USER_DIR, "mods");
String jar;
if (vs == null) {
jar = "m.jar";
} else {
jar = "m-" + vs + ".jar";
}
createDummyJarFile(dir.resolve(jar), man);
ModuleFinder finder = ModuleFinder.of(dir);
assertTrue(finder.findAll().size() == 1);
assertTrue(finder.find(name).isPresent());
ModuleReference mref = finder.find(name).get();
ModuleDescriptor descriptor = mref.descriptor();
assertEquals(descriptor.name(), name);
assertEquals(descriptor.version()
.map(ModuleDescriptor.Version::toString)
.orElse(null), vs);
}
示例12: testCreateModule
import java.util.jar.Attributes; //导入方法依赖的package包/类
public void testCreateModule() {
String codeName = "test-module";
String specificationVersion = "1.0";
URL distribution = null;
try {
new URL ("http://netbeans.de/module.nbm");
} catch (MalformedURLException ex) {
fail (ex.getMessage ());
}
String author = "Jiri Rechtacek";
String downloadSize = "12";
String homepage = "http://netbeans.de";
Manifest manifest = new Manifest ();
Attributes mfAttrs = manifest.getMainAttributes ();
mfAttrs.put (new Attributes.Name ("OpenIDE-Module"), "org.myorg.module/1");
mfAttrs.put (new Attributes.Name ("OpenIDE-Module-Implementation-Version"), "060216");
mfAttrs.put (new Attributes.Name ("OpenIDE-Module-Long-Description"), "Real module Hello installs Hello menu item into Help menu.");
mfAttrs.put (new Attributes.Name ("OpenIDE-Module-Module-Dependencies"), "org.openide.util > 6.9.0.1");
mfAttrs.put (new Attributes.Name ("OpenIDE-Module-Name"), "module");
mfAttrs.put (new Attributes.Name ("OpenIDE-Module-Requires"), "org.openide.modules.ModuleFormat1");
mfAttrs.put (new Attributes.Name ("OpenIDE-Module-Specification-Version"), "1.0");
UpdateLicense license = UpdateLicense.createUpdateLicense ("none-license", "no-license");
UpdateItem result = UpdateItem.createModule(codeName,
specificationVersion,
distribution, author, downloadSize,
homepage, null, "test-category",
manifest, false, false, true, true, "my-cluster",
license);
assertNotNull ("Module UpdateItem was created.", result);
}
示例13: transform
import java.util.jar.Attributes; //导入方法依赖的package包/类
InputStream transform(InputStream inputStream) throws MojoExecutionException {
try (InputStream in = inputStream) { // No need for new variable in Java 9.
Manifest manifest = new Manifest(in);
Attributes attributes = manifest.getMainAttributes();
if (!attributes.containsKey(PREMAIN_CLASS)) {
throw new MojoExecutionException(PREMAIN_CLASS + " not found in MANIFEST.MF. This is a bug in promagent-maven-plugin.");
}
attributes.put(CREATED_BY, pluginArtifactId + ":" + pluginVersion);
ByteArrayOutputStream out = new ByteArrayOutputStream();
manifest.write(out);
return new ByteArrayInputStream(out.toByteArray());
} catch (IOException e) {
throw new MojoExecutionException("Failed to transform MANIFEST.MF: " + e.getMessage(), e);
}
}
示例14: testGoodESVersionInJar
import java.util.jar.Attributes; //导入方法依赖的package包/类
/** make sure if a plugin is compiled against the same ES version, it works */
public void testGoodESVersionInJar() throws Exception {
Path dir = createTempDir();
Manifest manifest = new Manifest();
Attributes attributes = manifest.getMainAttributes();
attributes.put(Attributes.Name.MANIFEST_VERSION, "1.0.0");
attributes.put(new Attributes.Name("X-Compile-Elasticsearch-Version"), Version.CURRENT.toString());
URL[] jars = {makeJar(dir, "foo.jar", manifest, "Foo.class")};
JarHell.checkJarHell(jars);
}
示例15: testBadESVersionInJar
import java.util.jar.Attributes; //导入方法依赖的package包/类
/** make sure if a plugin is compiled against a different ES version, it fails */
public void testBadESVersionInJar() throws Exception {
Path dir = createTempDir();
Manifest manifest = new Manifest();
Attributes attributes = manifest.getMainAttributes();
attributes.put(Attributes.Name.MANIFEST_VERSION, "1.0.0");
attributes.put(new Attributes.Name("X-Compile-Elasticsearch-Version"), "1.0-bogus");
URL[] jars = {makeJar(dir, "foo.jar", manifest, "Foo.class")};
try {
JarHell.checkJarHell(jars);
fail("did not get expected exception");
} catch (IllegalStateException e) {
assertTrue(e.getMessage().contains("requires Elasticsearch 1.0-bogus"));
}
}