
在编写代码时使用的是绝对路径来访问的这个文件,然而这个文件是在jar包中的,jar包中有自己的一套Url编址:jar:<url>!/{entry})。所以导致运行时无法访问到文件。
解决办法就是在构造File对象时使用url来构造,而文件的url获取使用ClassLoader
URL fileURL=thisgetClass()getResource("0txt");
File file = new File(fileURL);
FileInputStream fis = new FileInputStream(file);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fis));
看大家你一言我一语的,估计楼主也不明白,
楼主都说自己是听说了,说明不太懂。切实的,部署到weblogic里会把classes打成jar包
不是这么个意思。
是这样的,听我说,weblogic会把应用打成war包,没有把classes打成jar包这么回事儿,这点要区分清楚。
应用包括组件,jsp页面,和WEB-INF目录。
2、你要去1txt,假设,路径是这样子哦:application/WEB-INF/classes
那么,你的war包的应用就是application,当然这个application是存在在weblogic创建的域中的,
JDK14对应: String path = requestgetRealPath("/");
JDK16对应 String path = requestgetSession()getServletContext()getRealPath("");
这句代码就取到了application,所以你的最终路径就是path = path + "
WEB-INF/classes/cyc/1txt"
java判断方法来自哪个jar包主要是使用class的getSimpleName()方法,代码如下:
package comqiulinhe;public class CeShi {
public static void main(String[] args) {
Class<> clazz = Stringclass;//String类
Systemoutprintln(clazzgetResource(clazzgetSimpleName() + "class"));
}
}
结果如下:
如果你看过一些框架的源码,就知道大多数情形时他们都是先找到某个包的对应的文件位置,并遍历其下面的文件,读取其文件名,对应的就是类名。
/@(#)PackageUtiljava 100 2006-11-27
Copyright (c) 2005 Shenzhen Surfilter Network Technology Co,Ltd All rights reserved
/
package orgrutcore;
import javaioFile;
import javaioFileInputStream;
import javaioIOException;
import javautilArrayList;
import javautilList;
import javautiljarJarEntry;
import javautiljarJarInputStream;
/
@since 2006-11-27
@author wushugen
Modified History:
/
public class PackageUtil {
/
@param args
@throws IOException
/
public static void main(String[] args) throws IOException {
List<String> cls = getClassInPackage("javautil");
for(String s:cls){
Systemoutprintln(s);
}
}
public static List<String> getClassInPackage(String pkgName) {
List<String> ret = new ArrayList<String>();
String rPath = pkgNamereplace('', '/') + "/";
try {
for (File classPath : CLASS_PATH_ARRAY) {
if(!classPathexists()) continue;
if (classPathisDirectory()) {
File dir = new File(classPath, rPath);
if(!direxists()) continue;
for (File file : dirlistFiles()) {
if (fileisFile()) {
String clsName = filegetName();
clsName = pkgName+"" +clsNamesubstring(0, clsNamelength() - 6);
retadd(clsName);
}
}
} else {
FileInputStream fis = new FileInputStream(classPath);
JarInputStream jis = new JarInputStream(fis, false);
JarEntry e = null;
while ((e = jisgetNextJarEntry()) != null) {
String eName = egetName();
if (eNamestartsWith(rPath) && !eNameendsWith("/")) {
retadd(eNamereplace('/', '')substring(0,eNamelength()-6));
}
jiscloseEntry();
}
jisclose();
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return ret;
}
private static String[] CLASS_PATH_PROP = { "javaclasspath", "javaextdirs",
"sunbootclasspath" };
private static List<File> CLASS_PATH_ARRAY = getClassPath();
private static List<File> getClassPath() {
List<File> ret = new ArrayList<File>();
String delim = ":";
if (SystemgetProperty("osname")indexOf("Windows") !=-1)
delim = ";";
for (String pro : CLASS_PATH_PROP) {
String[] pathes = SystemgetProperty(pro)split(delim);
for (String path : pathes)
retadd(new File(path));
}
return ret;
}
}
以上就是关于JAVA读取了本地TXT,但是导出JAR并运行时提示系统找不到指定路径求助全部的内容,包括:JAVA读取了本地TXT,但是导出JAR并运行时提示系统找不到指定路径求助、一个Java项目部署到weblogic里,听说weblogic会把classes目录打成jar包,怎么获取classes里文件的路径、java 判断方法来自哪个jar包等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)