我不能使用“ print / println”功能

时间:2018-11-21 14:15:23

标签: java

我正在使用IntelliJ IDEA。 我已经完成了谷歌搜索为我提供的所有潜在解决方案。 我已经尝试过执行File-> Invalidate Caches / Restart,它仍然无法解决问题。

这是完整的代码; 这是从OCI文档中复制的,我已经对代码进行了一些修改,但是我不知道如何解决。 我是大三,是的,但是我要为自己开发的大量文档存在此类问题而感到烦恼。

这是完整的代码。

package com;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.security.SecureRandom;
import java.security.cert.Certificate;
import java.security.cert.X509Certificate;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.commons.codec.binary.Base64;
import static java.lang.System.out;

import static com.sun.org.apache.xalan.internal.xsltc.cmdline.Compile.printUsage;


public class ociAuth {
    private static String server;
    private static String user;
    private static String password;
    private static String port = "8443";
    private static String response_format = "json";
    private static String server_url;


    public static void main(String[] args) {
        if(args.length < 3 || args.length > 4) {
            printUsage();
            System.exit(1);
        }

        setUserArguments(args);
        server_url = "https://" + server + ":" + port + "/rest/v1/assets/storages";
        try {
            HttpsURLConnection connection =
                    getAllTrustingHttpsUrlConnection();
            if(connection == null) {
                System.err.println("FATAL: Failed to create HTTPS connection to URL: " + server_url);
                System.exit(1);
            }
            System.out.println("Invoking API: " + server_url);
            connection.setRequestMethod("GET");
            connection.setRequestProperty("Accept", "application/" + response_format);
            String authString = getAuthorizationString();
            connection.setRequestProperty("Authorization", "Basic " +
                    authString);
            if (connection.getResponseCode() != 200) {
                System.err.println("API Invocation Failed : HTTP error code : "
                        + connection.getResponseCode());
                System.exit(1);
            }
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    (connection.getInputStream())));
            String response;
            System.out.println("Response:");
            while ((response = br.readLine()) != null) {
                System.out.println(response);
            }
            connection.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

//THESE PRINTS HERE BELLOW
//THESE PRINTS HERE BELLOW
//THESE PRINTS HERE BELLOW

            System.out.print("\nUsage:\n\tHelloApiServices <api-server host[:port]> <user> <password> [json|xml]\n");
            System.out.print("\nExamples:\n\tHelloApiServices localhost admin mypassword");
            System.out.print("\tHelloApiServices 10.22.12.34:8320 admin password");
            System.out.print("\tHelloApiServices 10.22.12.34 admin password xml");
            System.out.print("\tHelloApiServices 10.22.12.34:8212 admin password xml\n");
            System.out.print("\nNote:\n\t(1) When port number is not provided, 8443 is chosen by default.");
            System.out.print("\t(2) When response format (json or xml) is not provided, json is chosen by default. \n");
}

//THESE PRINTS HERE ABOVE
//THESE PRINTS HERE ABOVE
//THESE PRINTS HERE ABOVE

    private static void setUserArguments(String[] args) {
        server = args[0];
        user = args[1];
        password = args[2];
        if(args.length == 4) {
            response_format = args[3];
            if(!response_format.equals("json") && ! response_format.equals("xml")) {
                        printUsage();
                System.exit(1);
            }
        }
        if(server.contains(":")) {
            String[] parts = server.split(":");
            server = parts[0];
            port = parts[1];
        }
    }

    private static HttpsURLConnection getAllTrustingHttpsUrlConnection() {
        HttpsURLConnection conn = null;
        try {
            TrustManager[] trustAllCertificatesManager = new
                    TrustManager[]{new X509TrustManager() {
                public X509Certificate[] getAcceptedIssuers() {
                    return
                            null;
                }

                public void checkClientTrusted(X509Certificate[]
                                                       certs, String authType) {
                }

                public void checkServerTrusted(X509Certificate[]
                                                       certs, String authType) {
                }
            }};

            SSLContext sslContext = SSLContext.getInstance("TLS");
            sslContext.init(null, trustAllCertificatesManager, new
                    SecureRandom());

            HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
            URL url = new URL(server_url);
            conn = (HttpsURLConnection) url.openConnection();
            conn.setHostnameVerifier(new HostnameVerifier() {
                public boolean verify(String host, SSLSession
                        session) {
                    return true;
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
        return conn;
    }
    private static String getAuthorizationString() {
        String userPassword = user + ":" + password;
        byte[] authEncodedBytes =
                Base64.encodeBase64(userPassword.getBytes());
        String ajdeovako = new String(authEncodedBytes);
        return ajdeovako;
    }
}

2 个答案:

答案 0 :(得分:0)

您有一个额外的括号。 System.out.print调用不在您的main方法内部,而是在方法和不应包含print调用的右括号之间。将打印调用向上移动到您的main方法中,它将可以正常编译。

答案 1 :(得分:0)

您还有一个},将其删除会解决您的错误。

    } <-- remove this

//THESE PRINTS HERE BELLOW
//THESE PRINTS HERE BELLOW
//THESE PRINTS HERE BELLOW

            System.out.print("\nUsage:\n\tHelloApiServices <api-server host[:port]> <user> <password> [json|xml]\n");
            System.out.print("\nExamples:\n\tHelloApiServices localhost admin mypassword");
            System.out.print("\tHelloApiServices 10.22.12.34:8320 admin password");
            System.out.print("\tHelloApiServices 10.22.12.34 admin password xml");
            System.out.print("\tHelloApiServices 10.22.12.34:8212 admin password xml\n");
            System.out.print("\nNote:\n\t(1) When port number is not provided, 8443 is chosen by default.");
            System.out.print("\t(2) When response format (json or xml) is not provided, json is chosen by default. \n");
}