-classpath我对选项在编译和运行java 程序中所扮演的角色感到困惑。请帮助我理解。
这是蚀.classpath文件的例子
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src/main/java"/>
<classpathentry kind="src" path="src/main/resources"/>
<classpathentry kind="src" output="bin/test" path="src/test/java"/>
<classpathentry kind="src" output="bin/test" path="src/test/resources"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="/kc-tools-thirdparty/lib/slf4j-api-1.7.7.jar"/>
<classpathentry kind="lib" path="/kc-tools-thirdparty/lib/slf4j-log4j12-1.7.7.jar"/>
<classpathentry kind="lib" path="/kc-tools-thirdparty/lib/commons-collections4-4.0.jar"/>
<classpathentry kind="lib" path="/kc-tools-thirdparty/lib/commons-codec-1.6.jar"/>
<classpathentry kind="lib" path="/kc-tools-thirdparty/lib/commons-io-2.4.jar"/>
<classpathentry kind="lib" path="/kc-tools-thirdparty/lib/commons-cli-1.2.jar"/>
<classpathentry kind="lib" path="/kc-tools-thirdparty/lib/commons-net-3.3.jar"/>
<classpathentry kind="lib" path="/kc-tools-thirdparty/lib/commons-lang-2.4.jar"/>
<classpathentry kind="lib" path="/kc-tools-thirdparty/lib/opencsv-2.3.jar"/>
<classpathentry kind="lib" path="/kc-tools-thirdparty/lib/log4j-1.2.17.jar"/>
<classpathentry kind="output" path="bin/main"/>
</classpath>
Run Code Online (Sandbox Code Playgroud)
在gradle中,我想自动将它们添加到依赖项中。
是否有任何插件可以做到这一点?(我知道我可以编写自己的方法来解析XML,但我正在寻找一些通用方法)
这是我的代码:
package biz.tugay.deleteNow;
/* User: koray@tugay.biz Date: 19/12/15 Time: 12:57 */
import com.sun.org.apache.xml.internal.dtm.ref.DTMNodeList;
import org.xml.sax.InputSource;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
public class TestClass {
public static void main(String[] args) throws XPathExpressionException, FileNotFoundException {
XPath xpath = XPathFactory.newInstance().newXPath();
String expression = "/library/catalog/book";
InputSource inputSource = new InputSource();
inputSource.setCharacterStream(new FileReader(new File("/Users/koraytugay/Desktop/widgets.xml")));
Object evaluate = xpath.evaluate(expression, inputSource, XPathConstants.NODESET);
DTMNodeList dtmNodeList = (DTMNodeList) evaluate;
System.out.println(dtmNodeList.getLength());
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的 pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> …Run Code Online (Sandbox Code Playgroud) 我知道如果我想从 Main 类运行 main,通过使用sourceSets.main.runtimeClasspath类路径,我必须将 Main 类放在里面src/main/java并使用类似的东西:
apply plugin: 'java'
dependencies {
}
task myTask (type: JavaExec){
dependsOn classes
classpath sourceSets.main.runtimeClasspath
main = 'Main'
}
Run Code Online (Sandbox Code Playgroud)
我想要的是了解如何指定不同的类路径,从中检索包含 main() 的类。如果我想从不在 src/main/java 中但与build.gradle.
我知道做这样的事情没有意义,但我希望找到一个解决方案作为学习 Gradle的练习。
我在 Eclipse 中有两个具有相同声明依赖项的项目。一组单元测试有效,另一组无效。我收到一个错误No tests matching [*methodname*]。一些谷歌搜索表明库问题,但我无法确定可以在哪里引入这些问题。项目几乎相同,测试框架(junit、powermock、mockito)应该相同。在首选项中查看 java 构建路径 -> java 构建路径显示相同的引用。我怀疑 eclipse 菜单没有反映在运行时加载的实际类。有没有办法在运行时回显类路径,以便我可以扫描它以查找重复项?
更新:潜在的问题是我在类路径上有 junit 和 powermock 的版本冲突。我能够通过从 eclipse 构建路径配置面板中删除、重新添加和重新排序 jar 来解决这个问题。这是非常乏味的,下面接受的解决方案会显着减少解决时间。
这个问题被标记为重复,但链接的文章特定于 maven,并没有解决在运行时看到类路径的潜在问题。公认的解决方案适用于 maven 和非 maven 项目。
我需要使用/定义java.io.File变量类型来获取将作为参数发送给另一个方法的文件。
现在我有相对路径:
File file = new File("C:/javaproject/src/main/resources/demo/test.txt");
Run Code Online (Sandbox Code Playgroud)
我想用ClassLoader这样的方式改变它:
ClassLoader.getSystemResource("/demo/test.txt");
Run Code Online (Sandbox Code Playgroud)
但我无法将它用于文件,因为不是同一类型。如果我使用.toString()它会返回 NullPointerException:
java.lang.NullPointerException: null
Run Code Online (Sandbox Code Playgroud)
当我用系统输出打印它时返回相同的异常: System.out.println(ClassLoader.getSystemResource("demo/test.txt").toString());
文件夹和文件都存在。为什么会出现这个错误?
我正在设置 Kafka 消费者配置,但配置在类路径上找不到密钥库或信任库:
@EnableKafka
@Configuration
public class KafkaConfig {
@Value("${kafka.ssl.keystore}")
private String keyStorePath;
@Value("${kafka.ssl.truststore}")
private String trustStorePath;
@Bean
public ConsumerFactory<String, String> getConsumerFactory() {
Map<String, Object> properties = new HashMap<>();
properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG,"my-bootstrap.mydomain.com:443");
properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
properties.put(ConsumerConfig.GROUP_ID_CONFIG, "group1");
properties.put(ConsumerConfig.CLIENT_ID_CONFIG, "client1");
properties.put("enable.auto.commit", "true");
properties.put("auto.commit.interval.ms", "500");
properties.put("session.timeout.ms", "30000");
properties.put(CommonClientConfigs.SECURITY_PROTOCOL_CONFIG, "SSL");
properties.put(SslConfigs.SSL_KEYSTORE_LOCATION_CONFIG, keyStorePath);
properties.put(SslConfigs.SSL_KEYSTORE_PASSWORD_CONFIG, "password");
properties.put(SslConfigs.SSL_TRUSTSTORE_LOCATION_CONFIG, trustStorePath);
properties.put(SslConfigs.SSL_TRUSTSTORE_PASSWORD_CONFIG, "password");
properties.put(SslConfigs.SSL_KEY_PASSWORD_CONFIG, "password");
return new DefaultKafkaConsumerFactory<>(properties);
}
@Bean
public ConcurrentKafkaListenerContainerFactory<String, String> kafkaListenerContainerFactory() {
ConcurrentKafkaListenerContainerFactory<String, String> factory
= new ConcurrentKafkaListenerContainerFactory<>();
factory.setConsumerFactory(getConsumerFactory());
return factory;
}
}
Run Code Online (Sandbox Code Playgroud)
密钥库和信任库都位于src/main/resources/ssl与配置类相同的 …
在 Scala 中,使用 sbt,我很好奇如果我:
case class User(name: String, age: Int)在两个地方,但一个有require(age >= 0))build.sbt我的具体问题是,这些课程在哪些方面被模糊选择?也就是说,在编译、发布和应用程序启动过程的哪些点上,流行的类可能会在一种定义和另一种定义之间交替?
这是针对 Play Framework 应用程序的,但我认为这没什么区别。这也适用于 SBT v0.13,它使用 Ivy 进行依赖管理。
我有一个关于Java类路径变量的问题.
如果我有多个具有相同类的jar,jvm在运行时使用哪个jars.Class Path中列出的第一个,最后一个,还是未定义的?
谢谢
在命令行上java -cp只是缩写java -classpath?
(我似乎记得他们可能有不同的行为,但找不到明确的文档).
更新感谢(@AlBlue)确认我的记忆确实是正确的并且他们曾经是不同的.