我正在尝试导出到具有以下结构的项目的可运行JAR:
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim ApiResult As String = ""
ApiResult = callAPI(1, 100)
ApiResult = ApiResult + callAPI(101, 100)
ApiResult = ApiResult + callAPI(201, 100)
logBox.Text = ApiResult
End Sub
Protected Function callAPI(startRecord As Integer, numRecords As Integer) As String
Dim apiXmlResponse As String = ""
'call api webservice
Dim apiUrl As String = "https://api.example.com/?startRecord=" + startRecord.ToString + "&maxRecords=" + numRecords.ToString
Dim apiRequest As WebRequest = WebRequest.Create(apiUrl)
apiRequest.Method = "POST"
apiRequest.ContentType = "text/xml"
'read api response
Dim apiResponse As WebResponse = apiRequest.GetResponse()
Dim apiStatus As String = CType(apiResponse, HttpWebResponse).StatusDescription.ToString
Dim dataStream As Stream = apiResponse.GetResponseStream()
Dim apiReader As New StreamReader(dataStream)
apiXmlResponse = apiReader.ReadToEnd()
Return apiXmlResponse
End Function
使用Project
|
|---Source folder
| |
| |
| .java files
|
|
|---Image folder
|
|
.png files
在.png
文件中读取.java
图像,这在我在Eclipse中运行程序时工作正常,但如果我导出到可运行的JAR,我会得到以下内容stacktrace中的异常:
ImageIO.read(new File("ImageFolder/image.png"));
以下是我在Eclipse中的项目的屏幕截图:
如何在我的JAR中加入javax.imageio.IIOException: Can't read input file!
at javax.imageio.ImageIO.read(Unknown Source)
文件夹,以便可以阅读cardImages
?
答案 0 :(得分:0)
它应该在你的罐子里提取。
只是提示,资源更喜欢使用
InputStream is = ClassLoader.getSystemResourceAsStream("resources/sample.txt");
答案 1 :(得分:0)
使用目录导出,使用流getClass().getResourceAsStream('cardmages/imagename');
通常可以运行的jar文件,文件操作对其路径是实际的,在这里它会得到很好的处理。 对于前
c:/myFolder>yourjar.jar
然后
您的文件试图从c:/myFolder/cardimages/imagename.
File是一个用于从文件系统访问文件的类。不是从罐子里面来的。