简体   繁体   English

当我可以在类路径中清楚地看到文件时,为什么Java编译器说我的文件不在类路径中?

[英]Why does the Java compiler say my file was not found in the class path when I can clearly see it in the class path?

I am using java -classpath $CLASSPATH ... , where $CLASSPATH has been set to /file1path/file1:/file2path/file2 and so on. 我正在使用java -classpath $CLASSPATH ... ,其中$CLASSPATH已设置为/file1path/file1:/file2path/file2 ,依此类推。 Despite this, Java complains that file1 is not found. 尽管如此,Java抱怨找不到file1 I tried to set -Dfile1=file:///fullpath/file1 , but it still says it cannot find the file. 我试图设置-Dfile1=file:///fullpath/file1 ,但是它仍然说找不到文件。 Is there any reason why this might happen other than that I am not seeing a simpler problem like a typo or something (which I have checked for many times)? 除了我没有看到像打字错误之类的更简单的问题(我已经检查了很多次)之外,是否有其他原因会导致这种情况发生?

More specifically, this.getClass().getClassLoader().getResourceAsStream(configurationFileName) is returning null . 更具体地说, this.getClass().getClassLoader().getResourceAsStream(configurationFileName)返回null

The file that is not being found is a configuration file (.properties), not a JAR file. 找不到的文件是配置文件(.properties),而不是JAR文件。

You set a classpath to point to a directory containing something or an archive containing resources. 您将classpath设置为指向包含内容的目录或包含资源的档案。 I don't believe you can add a resource directly to the classpath . 我不相信您可以直接将资源添加到classpath

Try setting your classpath to /file1path instead of /file1path/file1 尝试将classpath设置为/file1path而不是/file1path/file1

The classpath should specify the directory where your package hierarchy rooted. 类路径应指定软件包层次结构所在的目录。

package org.djna, file system : C:/myhome/javastuff/org/djna/Myclass.java 包org.djna,文件系统: C:/myhome/javastuff/org/djna/Myclass.java

classpath is set to c:/myhome/javastuff classpath设置为c:/myhome/javastuff

If you are trying to open files from your application using getResourceAsStream() or some such the the details of the path depend on whether or not the filename has a leading / . 如果您尝试使用getResourceAsStream()或类似方法从应用程序中打开文件,则路径的详细信息取决于文件名是否以/开头。 Read the docs caefully and all will become clear. 仔细阅读文档,一切都会变得清晰。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么Java编译器说我的类需要在单独的文件中定义? - Why does the Java compiler say my class needs to be defined in a separate file? 未找到 Java 本机文件,当本机文件明确位于 java.library.path 中时 - Java native file not found, when native file is clearly in java.library.path 如何从 Java 中 Main 类的相对路径读取文件? - How can I read a file from a path relative of my Main class in Java? 我在哪里可以放置我的java库,以便在使用javac时它们总是在类路径中? - Where can I put my java libraries so they will always be in the class path when using javac? 基于Java的Spring MVC Java配置无法看到类路径属性文件 - Spring MVC Java-based configuration can't see class-path properties file Java编译器看不到我的课程 - Java compiler cannot see my class "为什么我的 java 程序在类路径中明显存在时报告“找不到包”?" - Why does my java program report "package not found" when it's clearly there in the classpath? 找不到java.nio.file.Path的类文件 - class file for java.nio.file.Path not found .java文件到类名的路径 - .java file path to class name 当 JAR 文件位于 class 路径中时,未找到 JSON 简单 - JSON simple not found when JAR file is in class path
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM