简体   繁体   English

Intellij Idea-构建jar之后Java程序无法正常运行

[英]Intellij Idea - Java program doesn't work properly after building jar

I have a weird problem with java - I am programming in Intellij Idea, I am using this little code to make basic authorization to site 我在使用Java时遇到一个奇怪的问题-我正在Intellij Idea中进行编程,我正在使用此小代码对网站进行基本授权

        String authString = name + ":" + password;
        byte[] binaryData = authString.getBytes();
        String authStringEnc = new String(Base64.encodeBase64(binaryData));
        URL url = new URL(webPage);
        URLConnection urlConnection = url.openConnection();
        urlConnection.setRequestProperty("Authorization", "Basic " + authStringEnc);
        InputStream is_auth = urlConnection.getInputStream();
        InputStreamReader isr = new InputStreamReader(is_auth);

This code worked for some weeks, but I continued writing my program. 这段代码工作了几个星期,但我继续编写程序。 Then yesterday I saw that - program works when I click run in Intellij, but when I start a .jar file, this line cannot be done: 然后昨天我看到-当我单击在Intellij中运行时,程序可以工作,但是当我启动.jar文件时,此行无法完成:

        String authStringEnc = new String(Base64.encodeBase64(binaryData));

I don't know why, but program stops when doing this line. 我不知道为什么,但是程序在执行此行时停止。 But when I click run or debug in Intellij, everything is working. 但是,当我单击Intellij中的运行或调试时,一切正常。

Btw. 顺便说一句。 I am using this library for encodeBase64: 我将这个库用于encodeBase64:

        import org.apache.commons.codec.binary.Base64;

If you don't know why it isn't working, maybe you can share with me idea to make a basic authorization to site. 如果您不知道为什么它不起作用,也许您可​​以与我分享对站点进行基本授权的想法。 Thanks in advance. 提前致谢。

You need to add the Apache Commons Codec jar to your classpath when you run the jar file . 运行jar文件时,需要将Apache Commons Codec jar添加到类路径中 You should make sure that any other jar files/libraries that you have added in IntelliJ are also on the classpath. 您应该确保在IntelliJ中添加的其他jar文件/库也位于类路径中。

You should use logging in order to research your stacktrace. 您应该使用日志记录来研究堆栈跟踪。 Probably you have some ClassNotFound exception or something similarly obvious. 可能您有一些ClassNotFound异常或类似的明显现象。

The answer is: I only had to refresh jar artifact. 答案是:我只需要刷新jar工件。 I deleted old artifact and created it again, after this everything is working well. 一切正常之后,我删除了旧工件并再次创建了它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM