I have the code to add document to Solr. I tested it in Both Solr 6.6.2 and Solr 7.2.1 and failed.
import java.io.IOException; import org.apache.solr.client.solrj.SolrClient; import org.apache.solr.client.solrj.SolrServerException; import org.apache.solr.client.solrj.impl.HttpSolrClient; import org.apache.solr.common.SolrInputDocument; public class AddingDocument { public static void main(String args[]) throws Exception { String urlString ="http://localhost:8983/solr/Solr_example"; SolrClient Solr = new HttpSolrClient.Builder(urlString).build(); //Preparing the Solr document SolrInputDocument doc = new SolrInputDocument(); //Adding fields to the document doc.addField("id", "007"); doc.addField("name", "James Bond"); doc.addField("age","45"); doc.addField("addr","England"); //Adding the document to Solr Solr.add(doc); //Saving the changes Solr.commit(); System.out.println("Documents added"); } } The compilation is successful like below. javac -cp .:/opt/solr/solr-6.6.2/dist/solr-solrj-6.6.2.jar AddingDocument.java However, when I run it, it gave me some errors message confused. java -cp .:/opt/solr/solr-6.6.2/dist/solr-solrj-6.6.2.jar AddingDocument Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/Header at org.apache.solr.client.solrj.impl.HttpSolrClient$Builder.build(HttpSolrClient.java:892) at AddingDocument.main(AddingDocument.java:13)Caused by: java.lang.ClassNotFoundException: org.apache.http.Header at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 2 more What is wrong with it? Is this urlString correct? Any help is appreciated! Andy Tang