小编Chi*_*int的帖子

无法在可执行文件中访问资源

有人可以指出我在这里做错了什么.

我有一个小天气应用程序,生成并发送HTML电子邮件.使用下面的代码,当我从Eclipse运行它时,一切正常.我的电子邮件生成后,它可以访问我的图像资源,并发送包含附件的电子邮件.

但是,当我通过运行mvn install构建可执行jar 并使用java -jar NameOfMyJar.jar运行jar时,我的图像资源获得了java.io.FileNotFound异常.

我知道我必须对我如何访问我的图像资源做错了,我只是不明白为什么它在没有打包的情况下工作正常,但每当我将它打包到一个罐子里时都会爆炸.

任何建议都非常感谢.


我的项目布局 在此输入图像描述


我如何访问我的图像资源

//Setup the ATTACHMENTS
        MimeBodyPart attachmentsPart = new MimeBodyPart();
        try {
            attachmentsPart.attachFile("resources/Cloudy_Day.png");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   
Run Code Online (Sandbox Code Playgroud)

StackTrace

    Exception in thread "main" java.lang.RuntimeException: javax.mail.MessagingException: IOException while sending message;
  nested exception is:
    java.io.FileNotFoundException: resources/Cloudy_Day.png (No such file or directory)
    at Utilities.SendEmailUsingGmailSMTP.SendTheEmail(SendEmailUsingGmailSMTP.java:139)
    at Utilities.SendEmailUsingGmailSMTP.SendWeatherEmail(SendEmailUsingGmailSMTP.java:66)
    at Weather.Main.start(Main.java:43)
    at Weather.Main.main(Main.java:23)
Caused by: javax.mail.MessagingException: IOException while sending message;
  nested exception is:
    java.io.FileNotFoundException: …
Run Code Online (Sandbox Code Playgroud)

java jar jakarta-mail filenotfoundexception maven

7
推荐指数
1
解决办法
3740
查看次数

构建服务器是否需要安装Microsoft Office才能构建解决方案?

我在使用构建服务器构建C#解决方案时遇到问题.基本上,我们有一个运行Jenkins的持续集成框.我在Jenkins工作,使用MSBuild构建我的解决方案.尝试构建时,MSBuild会报告未知或缺少的程序集引用.

error CS0234: The type or namespace name 'Office' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?)
error CS0246: The type or namespace name 'Worksheet' could not be found (are you missing a using directive or an assembly reference?)
Run Code Online (Sandbox Code Playgroud)

我的解决方案有一个功能,它利用Excel中的一些对象.

问题是,我真的必须安装Microsoft Office,以便我的构建服务器能够简单地构建解决方案吗?构建服务器将不会使用我的应用程序,我只需要它来构建我的解决方案.

我在StackOverflow上看到了类似的帖子,同样的问题,但似乎有多个相互矛盾的答案说"是的,你必须安装Office".虽然其他答案说"不,你可以安装PIA可再发行组件."

示例::
在.NET中使用Microsoft.Office.Interop.Excel需要什么参考?
构建服务器中的Microsoft.Office.Interop.Excel参考
是否必须安装Microsoft Office 2007?
如何在没有安装MS Office的计算机上使用Microsoft.Office.Interop.Excel?

在服务器上,我已经完成了下载/安装此处找到的主互操作程序集Redistributable的过程:: http://www.microsoft.com/en-us/download/details.aspx?id=3508
但是没有解决任何问题.

当然,该解决方案在我们的开发机器上构建得很好,因为我们都安装了Microsoft Office.

当然,我们不必获得另一个Microsoft Office许可证并将其安装在我们的构建服务器上,以便简单地构建我们的解决方案,对吧?或者我们会吗?

谢谢阅读.

c# msbuild ms-office jenkins

6
推荐指数
0
解决办法
1550
查看次数

JavaFX setCursor() 不工作

我有一个 JavaFX 应用程序,我似乎无法更改 Cursor 并希望有人可以看看我的代码,也许让我知道我做错了什么。

发生的事情的基本流程:

  1. 单击按钮
  2. 执行单独的任务以从 REST API 检索数据。
  3. 在 TextArea 组件中显示检索到的数据。

在我创建任务并点击 REST API 之前,我正在调用:

myPrimaryStage.getScene().setCursor(Cursor.WAIT);
Run Code Online (Sandbox Code Playgroud)

然后,在完成我正在调用的任务后:

this.myController.getMyPrimaryStage().getScene().setCursor(Cursor.DEFAULT);
Run Code Online (Sandbox Code Playgroud)

但出于某种原因,我从未真正看到光标发生变化。该应用程序工作正常,但光标始终保持默认状态。

有人有什么建议吗?

注意:我在 for 循环中添加只是为了消耗一些时间,以便我可以查看任务是否刚刚完成得非常快,但即使在运行时,我也从未看到光标发生变化。所以我一定是做错了什么。

主程序

package application;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import logic.MyController;


public class Main extends Application {
    private AnchorPane rootLayout;

    @Override
    public void start(Stage primaryStage) {
        try {
            FXMLLoader loader = new FXMLLoader(Main.class.getResource("MainScreen.fxml"));
            rootLayout = (AnchorPane) loader.load();
            Scene scene = new Scene(rootLayout,400,400);

            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setScene(scene);

            //Store the primaryStage in …
Run Code Online (Sandbox Code Playgroud)

java javafx

5
推荐指数
1
解决办法
6644
查看次数

使用 Mockito 模拟 java.nio.file.Files 静态方法

我试图了解如何模拟静态方法,特别是来自静态 Files.class 的静态方法。

基本上,每当此行执行时:

 Files.newInputStream(this.getPathObj(), StandardOpenOption.READ);
Run Code Online (Sandbox Code Playgroud)

我希望它只返回一个作为 InputStream 实例的对象。

这是我的类,其中包含我试图模拟的静态方法。

public class JavaFileInput{

private Path path;

public JavaFileInput(Path path){
    this.path = path;
}

public InputStream getInputStream() throws IOException {
    return Files.newInputStream(this.getPathObj(), StandardOpenOption.READ);
}

public Path getPathObj() {
    return this.path;
}

}
Run Code Online (Sandbox Code Playgroud)

这是一些“伪”单元测试代码,显然不起作用,但我希望它描绘了我想要完成的事情的想法。

@Mock(name="path")
private Path mockedPath = Mockito.mock(Path.class);

@InjectMocks
private JavaFileInput javaFile_MockedPath;

@Before
public void testSetup(){
  javaFile_MockedPath = new JavaFileInput(mockedPath);
  MockitoAnnotations.initMocks(this);
}

@Test
public void getNewInputStreamTest(){
    //Setup
    Mockito.when(Files.newInputStream(mockedPathObj, StandardOpenOption.Read)).thenReturn(new InputStream());

    //Test
    InputStream outputValue = javaFile_MockedPath.getInputStream();

    //Validate
    assertTrue(outputValue instanceof InputStream);

} …
Run Code Online (Sandbox Code Playgroud)

java unit-testing mockito

5
推荐指数
1
解决办法
1万
查看次数