0

Am not able to run maven created jar file from command prompt. Am using below command to execute my jar file ElasticSearchUtility-1.0.0-SNAPSHOT.jar.

java -jar ElasticSearchUtility-1.0.0-SNAPSHOT.jar

Am getting the below message :

no main manifest attribute, in ElasticSearchUtility-1.0.0-SNAPSHOT.jar

I have following java file

Document.java  (POJO Class)
DocumentIndex   (Main Class)

I have compiled this maven project and generated .jar file. While am executing this .jar file , its not executing.

Please find my project structure

enter image description here

Updated pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ElasticSearchUtility</groupId>
<artifactId>ElasticSearchUtility</artifactId>
<version>1.0.0-SNAPSHOT</version>

<dependencies>
    <dependency>
        <groupId>org.elasticsearch</groupId>
        <artifactId>elasticsearch</artifactId>
        <version>6.1.2</version>
    </dependency>
    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>elasticsearch-rest-high-level-client</artifactId>
        <version>6.1.2</version>
    </dependency>
    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>rest</artifactId>
        <version>5.1.2</version>
    </dependency>

</dependencies>

<build>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <downloadSources>true</downloadSources>
                    <downloadJavadocs>false</downloadJavadocs>
                </configuration>
            </plugin>

            <!-- Set a compiler level -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

            <!-- Make this jar executable -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>**/log4j.properties</exclude>
                    </excludes>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>ProjectJar.project.App</mainClass>
                            <classpathPrefix>dependency-jars/</classpathPrefix>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <!-- Copy project dependency -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.5.1</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <!-- exclude junit, we need runtime dependency only -->
                            <includeScope>runtime</includeScope>
                            <outputDirectory>${project.build.directory}/dependency-
                                jars/</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            </plugins>
    </build>
    </project>

Please find my main class below.

public class DocumentIndex {

private final static String INDEX = "documents_local";  //Documents Table with file Path - Source
private final static String ATTACHMENT = "document_attachment_dev"; // Documents with Attachment...  -- Destination //document_attachment//
private final static String TYPE = "doc";
private static final Logger logger = Logger.getLogger(Thread.currentThread().getStackTrace()[0].getClassName());

public static void main(String args[]) throws IOException {


    RestHighLevelClient restHighLevelClient = null;
    Document doc=new Document();

    logger.info("Started Indexing the Document.....");

    try {
        restHighLevelClient = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http"),
                new HttpHost("localhost", 9201, "http")));
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }


    //Fetching Id, FilePath & FileName from Document Index. 
    SearchRequest searchRequest = new SearchRequest(INDEX); 
    searchRequest.types(TYPE);
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    QueryBuilder qb = QueryBuilders.matchAllQuery();
    searchSourceBuilder.query(qb);
    searchSourceBuilder.size(3000);
    searchRequest.source(searchSourceBuilder);
    SearchResponse searchResponse = null;
    try {
         searchResponse = restHighLevelClient.search(searchRequest);
    } catch (IOException e) {
        e.getLocalizedMessage();
    }

    SearchHit[] searchHits = searchResponse.getHits().getHits();
    long totalHits=searchResponse.getHits().totalHits;
    logger.info("Total Hits --->"+totalHits);

    int line=1;

    Map<String, Object> jsonMap ;
    for (SearchHit hit : searchHits) {

        String encodedfile = null;
        File file=null;

        Map<String, Object> sourceAsMap = hit.getSourceAsMap();
        doc.setId((int) sourceAsMap.get("id"));
        doc.setApp_language(sourceAsMap.get("app_language").toString());

        String filepath=doc.getPath().concat(doc.getFilename());

        logger.info("Line Number--> "+line+++"ID---> "+doc.getId()+"File Path --->"+filepath);

        try(PrintWriter out = new PrintWriter(new FileOutputStream(new File("d:\\AllFilePath.txt"), true))  ){
            out.println("Line Number--> "+line+"ID---> "+doc.getId()+"File Path --->"+filepath);
        }

        file = new File(filepath);
        if(file.exists() && !file.isDirectory()) {
            try {
                  try(PrintWriter out = new PrintWriter(new FileOutputStream(new File("d:\\AvailableFile.txt"), true))  ){
                        out.println("Line Number--> "+line+++"ID---> "+doc.getId()+"File Path --->"+filepath);
                    }
                FileInputStream fileInputStreamReader = new FileInputStream(file);
                byte[] bytes = new byte[(int) file.length()];
                fileInputStreamReader.read(bytes);
                encodedfile = new String(Base64.getEncoder().encodeToString(bytes));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }

        jsonMap = new HashMap<>();
        jsonMap.put("id", doc.getId());
        jsonMap.put("app_language", doc.getApp_language());
        jsonMap.put("fileContent", encodedfile);

        String id=Long.toString(doc.getId());

        IndexRequest request = new IndexRequest(ATTACHMENT, "doc", id )
                .source(jsonMap)
                .setPipeline(ATTACHMENT);


        try {
            IndexResponse response = restHighLevelClient.index(request);
        } catch(ElasticsearchException e) {
            if (e.status() == RestStatus.CONFLICT) {
            }
            e.printStackTrace();
        }

        line++;

    }

    logger.info("Indexing done.....");


}

}

Am getting below error while compile my pom.xml file

Failed to execute goal org.apache.maven.plugins:maven-dependency-plugin:2.5.1:copy-dependencies (copy-dependencies) on project ElasticSearchUtility: Error copying artifact from C:\Users\10641516\.m2\repository\org\apache\lucene\lucene-sandbox\7.1.0\lucene-sandbox-7.1.0.jar to D:\Karthikeyan\ElasticSearch\ElasticSearch_Tesing\target\dependency-
    [ERROR] jars\lucene-sandbox-7.1.0.jar: The filename, directory name, or volume label syntax is incorrect
Karthikeyan
  • 1,927
  • 6
  • 44
  • 109

1 Answers1

1

You have added the compiler plugin but you did not specify which class is your main class. That's why you get the exact error no main manifest attribute since the compiler can't find it. Because the main class serves as an entry point for your application it cannot be started.

To solve this, add your DocumentIndex main class to the configuration inside of the pom.xml. Inside of the configuration tag of your maven compiler plugin add:

    <archive>
      <manifest>
        <addClasspath>true</addClasspath>
        <classpathPrefix>lib/</classpathPrefix>
        <mainClass>com.mypackage.DocumentIndex</mainClass>
      </manifest>
    </archive>

See also this answer.

T A
  • 1,677
  • 4
  • 21
  • 29