9

I am trying to query contacts from salesForce account but getting above error.

I generated enterprise.jar file from the WSDL downloaded from my account.

I have wsc-23-min.jar along with enterprise.jar files in libs folder.

Below method is used to query contacts

private void queryContacts() {
    Log.i(TAG, "Querying for the 5 newest Contacts...");

    ConnectorConfig config = new ConnectorConfig();
    config.setUsername("***@***.com");
    config.setPassword("***");

    try {
        connection = Connector.newConnection(config);
        // query for the 5 newest contacts      
        com.sforce.soap.enterprise.QueryResult queryResults = connection.query("SELECT Id, FirstName, LastName, Account.Name " +
                "FROM Contact WHERE AccountId != NULL ORDER BY CreatedDate DESC LIMIT 5");

        if (queryResults.getSize() > 0) {
            for (int i=0;i<queryResults.getRecords().length;i++) {
                // cast the SObject to a strongly-typed Contact
                Contact c = (Contact)queryResults.getRecords()[i];
                Log.i(TAG, " ------------- Id: " + c.getId() + " - Name: "+c.getFirstName()+" "+
                        c.getLastName()+" - Account: "+c.getAccount().getName());
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}

Any help or clue is appreciated.

Thanks.

Edit:

I tried all the methods from this popular question but no luck.

I have others projects in my workspace which are having jar files (in 'libs' folder) and they all work properly. Don’t know whats wrong with salesforce related jar files.

Edit:

There is no problem with wsc-23.jar file. Only generated enterprise.jar through WSDL file causing problems.

Followed this to generate enterprise.jar file

Not getting what's wrong with it.

Edit:

I did clone git repository and got all respective jar files but when I run following command

java -classpath target/force-wsc-30.0.0-uber.jar com.sforce.ws.tools.wsdlc enterprise.wsdl enterprise.jar

getting

Error: Could not find or load main class com.sforce.ws.tools.wsdlc

I dont see tools folder in which wsdlc file should b thr in force-wsc-30.0.0-uber.jar which is causing above error.

If I clone that git repository, I am getting jar file generated but it wont be having tools folder and causes Error: Could not find or load main class com.sforce.ws.tools.wsdlc. If I download force-wsc-29.0.0.jar and try to run command, it throws error java.lang.ClassNotFoundException: org.stringtemplate.v4.STGroupDir.

I am not getting whats gng wrong here.

Stack trace when I download force-wsc-29.0.0.jar and try to build enterprice.jar file.

    basavaraj@basavaraj-u12-115:~/Android/library$ java -classpath force-wsc-29.0.0.jar com.sforce.ws.tools.wsdlc enterprise.wsdl enterprise.jar
Exception in thread "main" java.lang.NoClassDefFoundError: org/stringtemplate/v4/STGroupDir
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2521)
    at java.lang.Class.getMethod0(Class.java:2764)
    at java.lang.Class.getMethod(Class.java:1653)
    at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:494)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:486)
Caused by: java.lang.ClassNotFoundException: org.stringtemplate.v4.STGroupDir
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 6 more

Stack trace when I clone git repository and try build enterprice.jar file from generated force-wsc-30.0.0-uber.jar file.

basavaraj@basavaraj-u12-115:~/Android/library$ java -classpath force-wsc-30.0.0-uber.jar com.sforce.ws.tools.wsdlc enterprise.wsdl enterprise.jar
Error: Could not find or load main class com.sforce.ws.tools.wsdlc
10

Finally I got solution for my problem.

It was a matter of executing command with multiple libraries as in this link.

Along with force-wsc-29.0.0.jar from here, download rhino js-1.7R2.jar from here and string template ST-4.0.7.jar file from here.

Navigate to where these files are placed.

Then, run following command

$ java -classpath force-wsc-29.0.0.jar:js-1.7R2.jar:ST-4.0.7.jar com.sforce.ws.tools.wsdlc enterprise.wsdl enterprise.jar

Resulting enterprise.jar file will be generated in current working directory.

That's it!.

Thanks to Abhinav Gupta and Andrew Fawcett for their continuous support.

  • 1
    Thanks also - no mention of this anywhere, but it's crucial. – Caspar Harmer Mar 10 '16 at 7:40
3

What is your classpath, is it just libs folder or all jars in the lib folder. I think you need to add both wsc and enterprise jar files to the classpath explicitly.

Edit I realised after checking new github repo for WSC, that it moved to v30 from v23.0, that should hardly make a difference to problem. But as its always good to use latest stable lib, I gave a shot with github repo for WSC and things were working for me. Link to new repo : https://github.com/forcedotcom/wsc

Here are the steps you need(adding here, as some steps are not correct on repo README, I have to differ from them to make it work):

1) Clone repo :

$ git clone https://github.com/forcedotcom/wsc.git

2) MVN build

$ mvn clean package

3) Generate enterprise jar for your wsdl using following command, please replace paths as needed

$ java -classpath target/force-wsc-30.0.0-uber.jar com.sforce.ws.tools.wsdlc ~/Desktop/enterprise.wsdl enterprise.jar

4) Create a Java project, add WSC jar(force-wsc-30.0.0-uber.jar) and enterprise.jar to buidpath/classpath. Try running following code, it should be compilable:

import com.sforce.soap.enterprise.Connector;
import com.sforce.soap.enterprise.EnterpriseConnection;
import com.sforce.soap.enterprise.sobject.Contact;
import com.sforce.ws.ConnectionException;
import com.sforce.ws.ConnectorConfig;

public class EntTest {

    public static void main(String[] args) throws ConnectionException {
        ConnectorConfig config = new ConnectorConfig();
        config.setUsername("username");
        config.setPassword("password");

        EnterpriseConnection connection = Connector.newConnection(config);

        // query for the 5 newest contacts      
        com.sforce.soap.enterprise.QueryResult queryResults = connection.query("SELECT Id, FirstName, LastName, Account.Name " +
                "FROM Contact WHERE AccountId != NULL ORDER BY CreatedDate DESC LIMIT 5");

        if (queryResults.getSize() > 0) {
            for (int i=0;i<queryResults.getRecords().length;i++) {
                // cast the SObject to a strongly-typed Contact
                Contact c = (Contact)queryResults.getRecords()[i];
                System.out.println(" ------------- Id: " + c.getId() + " - Name: "+c.getFirstName()+" "+
                        c.getLastName()+" - Account: "+c.getAccount().getName());
            }
        }
    }
}

Let me know if you run into any more issues.

  • Its 'libs' folder and all jars (wsc-23.jar and enterprise.jar) r in that folder. – Braj Dec 18 '13 at 6:34
  • try adding all jars explicitly in classpaths, as libs folder will only help picking exploded class files, not individual jar files. – Abhinav Gupta Dec 18 '13 at 7:54
  • Tried. Still same error :( – Braj Dec 19 '13 at 6:47
  • I edited my question. Please have a look. – Braj Dec 20 '13 at 10:38
  • I just checked the WSC page, seems latest version is 30, why are you using old one ? Seems its from google code obsolete repo, use the one available on github here: github.com/forcedotcom/wsc – Abhinav Gupta Dec 20 '13 at 15:17
0

force-wsc-36.1.1.jar;
rhino-1.7.7.jar;
ST-4.0.8.jar

for JDK 1.7 would have to be added in classpath for SOAP 36.1.1 version

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.