Error on java application

2.9k views Asked by At

I have trying running a java application but it give me error like this

/home/irvank/.cache/netbeans/8.0.2/executor-snippets/run.xml:48: 
Cancelled by user.

Here is my code so far, RetrieveUserAttributes.java:

import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;

public class RetrieveUserAttributes {

    public static void main(String[] args) {
        RetrieveUserAttributes retrieveUserAttributes = new RetrieveUserAttributes();
        retrieveUserAttributes.getUserBasicAttributes("testuser", retrieveUserAttributes.getLdapContext());
    }

    public LdapContext getLdapContext(){
        LdapContext ctx = null;
        try{
            Hashtable<String, String> env = new Hashtable<String, String>();
            env.put(Context.INITIAL_CONTEXT_FACTORY,
                    "com.sun.jndi.ldap.LdapCtxFactory");
            env.put(Context.SECURITY_AUTHENTICATION, "Simple");
            env.put(Context.SECURITY_PRINCIPAL, "your username");
            env.put(Context.SECURITY_CREDENTIALS, "Password");
            env.put(Context.PROVIDER_URL, "ldap://serverlocation:389");
            ctx = new InitialLdapContext(env, null);
            System.out.println("Connection Successful.");
        }catch(NamingException nex){
            System.out.println("LDAP Connection: FAILED");
            nex.printStackTrace();
        }
        return ctx;
    }

    private User getUserBasicAttributes(String username, LdapContext ctx) {
        User user=null;
        try {

            SearchControls constraints = new SearchControls();
            constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
            String[] attrIDs = { "distinguishedName",
                    "sn",
                    "givenname",
                    "mail",
                    "telephonenumber"};
            constraints.setReturningAttributes(attrIDs);
            //First input parameter is search bas, it can be "CN=Users,DC=YourDomain,DC=com"
            //Second Attribute can be uid=username
            NamingEnumeration answer = ctx.search("DC=YourDomain,DC=com", "sAMAccountName="
                    + username, constraints);
            if (answer.hasMore()) {
                Attributes attrs = ((SearchResult) answer.next()).getAttributes();
                System.out.println("distinguishedName "+ attrs.get("distinguishedName"));
                System.out.println("givenname "+ attrs.get("givenname"));
                System.out.println("sn "+ attrs.get("sn"));
                System.out.println("mail "+ attrs.get("mail"));
                System.out.println("telephonenumber "+ attrs.get("telephonenumber"));
            }else{
                throw new Exception("Invalid User");
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return user;
    }

}

I am using Netbeans.

I can't seem to figure out what the problem is, why doesn't it compile?

The row in the xml is:

<translate-classpath classpath="${classpath}" targetProperty="classpath-translated" />
1

There are 1 answers

0
hagrawal7777 On

There is nothing wrong with your RetrieveUserAttributes.java, it is problem in XML file, and truly it is not problem in XML file but a IDE problem.

Same problem I have seen with Eclipse 4.3.2. This tell that something is corrupted in your workspace, so either restart your IDE, and if not then try to create a fresh workspace.