0

So, the problem is: I cannot load the imports. Help...

I got this code from online just to experiment with how it works. The problem is that the error that a "class, interface, or enum expected." It just won't compile and keeps asking for a JAR due to the dependencies at the top. I don't even know what dependencies were until I saw this program. Can someone please tell me why it's not working, and attempt to fix it? I tried all the .JAR Intellij suggested, then I tried searching up the issue but most of the answers were irrelevant or too complicated involving Maven or something. Please help...

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.5</version>
</dependency>

import org.apache.http.HttpHost;
import org.apache.http.HttpStatus;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.NTCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;


public class HttpDocumentExistsWithHttpClient {

/**
 * check if a document exists in a sharepoint library
 */
public static void main(String[] args) throws Exception{
    CloseableHttpClient httpclient = HttpClients.custom()
            .setRetryHandler(new DefaultHttpRequestRetryHandler(0,false))
            .build();


    String user = "myusername";
    String pwd = "mypassword";
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(AuthScope.ANY,
            new NTCredentials(user, pwd, "", ""));

    // You may get 401 if you go through a load-balancer.
    // To fix this, go directly to one of the sharepoint web server or
    // change the config. See this article :
    // http://blog.crsw.com/2008/10/14/unauthorized-401-1-exception-calling-web-services-in-sharepoint/
    HttpHost target = new HttpHost("web01.mysharepoint.local", 80, "http");
    HttpClientContext context = HttpClientContext.create();
    context.setCredentialsProvider(credsProvider);

    // The authentication is NTLM.
    // To trigger it, we send a minimal http request
    HttpHead request1 = new HttpHead("/");
    CloseableHttpResponse response1 = null;
    try {
        response1 = httpclient.execute(target, request1, context);
        EntityUtils.consume(response1.getEntity());
        System.out.println("1 : " + response1.getStatusLine().getStatusCode());
    }
    finally {
        if (response1 != null ) response1.close();
    }

    // The real request, reuse authentication
    String file = "/30500C/PubDoc/TEST/jira.log";  // source
    HttpGet request2 = new HttpGet("/_api/web/GetFileByServerRelativeUrl('" + file + "')/Etag");
    CloseableHttpResponse response2 = null;
    try {
        response2 = httpclient.execute(target, request2, context);
        EntityUtils.consume(response2.getEntity());
        int rc = response2.getStatusLine().getStatusCode();
        String reason = response2.getStatusLine().getReasonPhrase();
        if (rc != HttpStatus.SC_OK) {
            System.out.println(file + " is missing.  Reason : "
                    + reason + "   rc : " + rc + "(500 is the equivalent of NOT FOUND)");
        }
        else {
            System.out.println(file + " exists.");
        }
    }
    finally {
        if (response2 != null) response2.close();
    }
    return;
}
}

I'm literally just trying to compile this in JGrasp and IntelliJ. Not that experienced with Java.

1 Answers1

0

First of all, Maven is a dependency management and build tool, which can download dependencies automatically for you, if it is set-up.

The first piece of code (<dependency>...) are instructions to maven to fetch the dependencies. it belongs into a pom.xml file not in your class.

If you are not set-up for this and don't want to get into it you can download the needed JARs at https://hc.apache.org/downloads.cgi. And make sure to add this to the classpath.

I do recommend that you get familiar with a tool like Maven as you will encounter dependencies all along when working with Java and it does simplify a lot of things in the long run.

ced-b
  • 3,957
  • 1
  • 27
  • 39
  • Okay, that makes sense. Would I need to download all of the .zip JARs from the link your provided and place them under External Libraries of my project? Also, for the future, would it be wise to simply always create a Maven Project even if I'm doing simple code so that I have access to the build.xml file for dependencies just in case? – Simmon Thind Jun 08 '17 at 18:36
  • It seems like they have whole bunch of items bundled. So you will have to unzip the file and take the JARs out of the `lib` directory and stick that into your external dependencies folder. – ced-b Jun 08 '17 at 18:42
  • `build.xml` is for Ant build tool. It's best not to mix with Maven build tool unless you are forced to e.g. building an existing project. Really I think the above answer should refer to the Maven pom.xml file and not mention `build.xml` at all. – D-Dᴙum Jun 08 '17 at 18:58
  • @Kerry correct, I got mixed up. I fixed it. – ced-b Jun 08 '17 at 19:04
  • Okay, so I downloaded the files. However, they do not include a JAR. In my project, I have a .idea folder, .out, .src, and external libraries folder (that includes all the .JARs. I am absoloutely lost as to where I put these files I just downloaded as they are just folders and include .POM files. Is there any way you can help? I just need to make this program work. I'm only a first year student at my first internship, so I don't know anything... – Simmon Thind Jun 08 '17 at 19:05
  • If you download 4.5.3.zip it definitley has a `lib` directory, with a whole slew of jars. – ced-b Jun 08 '17 at 19:10
  • Oh, I accidently download the source file. My mistake, sorry. – Simmon Thind Jun 08 '17 at 19:15
  • UPDATE: So I got the files and put them into the JDK\bin\lib folders. At first it didn't work, but then I had to select the specific libraries to use from within the IDE manually... – Simmon Thind Jun 08 '17 at 19:33