- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个依赖 jar 文件的简单项目。 Jar 文件有一个带有构造函数的类,该构造函数接受 props.xml 的路径。
这是项目结构:
这是主类:
import com.file.reader.FileReader;
public class SimpleExample {
public static void main(String[]args){
FileReader rd = new FileReader("props.xml");
}
}
这是 FileReader.java
package com.file.reader;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
public class FileReader {
public FileReader(String fileName){
try {
File file = new File(fileName);
DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance()
.newDocumentBuilder();
Document doc = dBuilder.parse(file);
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
if (doc.hasChildNodes()) {
System.err.println((doc.getChildNodes()));
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
这基本上读取 xml 文件。
FileReader.java 是我的项目中正在访问的 jar 文件。当我在 Eclipse 中运行时,我看到以下输出:
[#document: null]
Root element :company
但是当我将 DummyFilePath 导出为 jar 文件并尝试从命令行运行时。
我看到正在抛出错误:
C:\Users\javaMan\props.xml (The system cannot find the file specified)
从命令行我正在运行
Java -jar DummyFilePath.jar
如何让它通过命令行运行
编辑
检查了一些链接的问题后,我尝试了不同的方法:
我将 props.xml 移至 src 文件夹。
然后我更改了 SimpleExample.java 如下:
import java.io.File;
import java.net.URL;
import com.file.reader.FileReader;
public class SimpleExample {
public static void main(String[] args) {
SimpleExample se = new SimpleExample();
System.err.println(se.getPath());
FileReader rd = new FileReader(se.getPath());
}
public String getPath(){
URL url1 = getClass().getClassLoader().getResource("props.xml");
File f = new File(url1.getFile());
return f.getAbsolutePath();
}
}
所以当我在 eclipse 中运行时,我看到下面的内容很好:
C:\Users\javaMan\Perforce\DummyFilePath\bin\props.xml
[#document: null]
Root element :company
当我运行相同的 DummyFilePath.jar 时,我看到以下错误:
C:\Users\javaMan\Desktop>java -jar "C:\Users\javaMan\Desktop\DummyFilePath.jar"
C:\Users\javaMan\Desktop\file:\C:\Users\javaMan\Desktop\DummyFilePath.jar!\props.xml
C:\Users\javaMan\Desktop\file:\C:\Users\javaMan\Desktop\DummyFilePath.jar!\props.xml (The filename, directory name, or volume label syntax is incorrect)
最佳答案
由于您只为 File
类提供了一个文件名(间接通过构造函数),因此它假定您的意思是它是相对路径(相对于当前目录)。换句话说,它相当于 .\props.xml
,并且由于命令行上的当前目录是 C:\Users\javaMan\
(当您执行 Java -jar DummyFilePath.jar
时,您可以在命令提示符左侧看到该目录),因此它会在那里查找。您可能需要指定 props.xml
的绝对文件路径。
例如,如果 props.xml
位于 C:\Users\javaMan\someotherfolder
中,则绝对路径(至少在 Windows 中)将为 C:\Users\javaMan\someotherfolder\props.xml
。
关于java - 如果我希望它通过命令行将项目作为 jar 文件运行,某些属性文件的路径应该是什么样子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16679746/
对于我不断地用刽子手问题来打扰这个论坛,我深表歉意。我希望很快就能完成这件事。确实在这方面一直在努力。这是到目前为止我的代码: #include #include #include #defin
这篇文章是 this one 的延续. 我试图了解我是否是唯一一个错过并需要 .NET 泛型类型继承其泛型参数类型之一的能力的人。 面临的挑战是收集支持此功能的令人信服的理由,或者,要知道没有。 我给
我正在使用下面的代码设计一个 3 小时的 JavaScript 倒计时 var count = 10800; var counter = setInterval(timer, 1000); //1
我正在处理验证并放置一个 html 模式属性,并希望在不点击保存/提交按钮的情况下查看错误消息。当我在字段外单击时,它应该根据我的模式条件进行验证。 这是我的代码: Extra Credit Da
我有一个像这样的 mysql 表结构: id int primary key name varchar start_time float 数据可能是这样的: id name
我试图在桌面上保留一张带有描述的图片,然后在移动设备上希望图片在左边,描述在右边……更多的是尽可能让所有内容都在首屏。 我希望桌面上的 DIV 是: 一个一个 BBB 但是在手机上: A B A B
我通过 Stackoverflow 进行了搜索,发现很多人试图避免 CSS 列中的重叠,但实际上我被要求让文本和图像重叠。 更新: 更好的图像解释了我被要求做的事情,以及指向我正在使用的代码的链接。谢
我在我的框架中设置了几个协议(protocol)来处理资源。在其中一个协议(protocol)中,我设置了一个扩展来为 decode 函数提供默认实现。显示代码和发生的情况更简单(请参阅对 fatal
我想同时加载,或者在上面的图像之后立即加载。它会在图像和标题 div 在预加载器内旋转之前不断加载 - 不好!这是 HTML: ✕ Various inv
我目前正在开发一个小游戏。我使用 getKeyCode 来移动我的角色,但问题是我不希望你在按住按钮的情况下能够继续移动。无论如何,我是否可以使用 getKeyCode 仅在第一次单击时注册,然后在释
我需要设置 width 或 max-width 才能让 margin:auto 工作,对吗? Like in this jsFiddle . 问题是,段落宽度等于 max-width,因此文本 blo
您好,我在同一页面上有一个表单和一个 Paypal 订阅按钮。在重定向到 Paypal 付款之前,我想在单击提交表单(通过电子邮件发送给我)时订阅按钮。 > >
我想让用户在点击一个文本字段时感觉就像我们点击一个按钮,在释放按钮后它会在很短的时间内变成橙色,然后闪烁并再次变成它的第一种颜色。当我点击它时它运行良好但没有出现颜色闪烁。 布局文件 主要 Ac
我怎样才能让我的 MediaPlayer 继续播放,即使在手机锁定和屏幕关闭的情况下,我认为它可能必须做一些使其成为服务但不确定的事情。如果是这样,我该如何将其更改为服务,或者是否有更快更简单的修复方
有人可以帮助我理解内存泄漏的概念以及特定数据结构如何促进/防止它(例如链表、数组等)。前段时间我被两个不同的人教过两次——由于教学方法的不同,这让我有点困惑。 最佳答案 维基百科有一个 good de
我希望 HTML 表单在提交后不执行任何操作。 action="" 不好,因为它会导致页面重新加载。 基本上,我希望在按下按钮或有人在输入数据后点击 Enter 时调用 Ajax 函数。是的,我可以删
我正在执行一个程序 say A from another,首先是 fork-ing,然后是 execve()。现在的问题是我希望 A 使用我通常使用 LD_PRELOAD 来完成的库。我如何在 exe
我目前正在为我们公司测试 Autofac。 我们希望有以下规则: 如果接口(interface)只实现了一次,则使用 builder.RegisterAssemblyTypes 自动添加它(见下文)。
正如 Scott Meyers 和 Andrei Alexandrescu 在 this 中概述的那样文章 简单尝试实现双重检查锁定实现在 C++ 中特别是不安全的,并且通常在不使用内存屏障的多处理器
目前我有一些代码可以在点击时改变图像,但这段代码是在一个 php HTML 循环中所以事情变得棘手,点击一个开关会使每个故事的开关都激活,因为它是基于 CSS 运行的.如何让它们独立交换? $("
我是一名优秀的程序员,十分优秀!