Calling the ElasticSearch Main()

Hi Team,

Im trying to run elastic search via eclipse.Im running this within a project with elastic search as dependency . I have the dependency specified in the pom.xml. Im trying to call the Elasticsearch.main().

Im not sure what arguments needs to be passed as string array. Because of this Im getting the below exception

Error: Could not find or load main class Des.path.home

I have also updated the vm arguments to

Des.path.home = D:\Resources\elasticsearch-2.2.1 -des. security.manager.enabled = false -Des.http.cors.enabled = True -Des.http.cors.allow-origin = *

But still Im getting the above error.

Any Help on this is much appreciated.

Hi @Vajb12,

are you calling Elasticsearch.main() from your Java code or do you want to create a run configuration?

If it is the former then I am sorry to say that this is not supported. Elasticsearch is meant to be started as a separate process and you should normally use the launch scripts for that.

If it is the latter, I am not a Eclipse user, but you need to define three things:

  • Main class: org.elasticsearch.bootstrap.Elasticsearch
  • Program Arguments: start
  • VM Arguments: -Des.path.home = D:\Resources\elasticsearch-2.2.1 -des. security.manager.enabled = false -Des.http.cors.enabled = True -Des.http.cors.allow-origin = *

Here is my configuration for Elasticsearch 2.x (single node) in IntelliJ which should give you an idea:

Daniel

1 Like

I have a question about -Des.path.home. What is that referring to? I'm not seeing a data folder. Is that something that I need to create and reference in the Edit Config?

Thanks

Jonathan

Hi @JonathanAaron,

with -Des.path.home you can specify the Elasticsearch home directory. Elasticsearch will read its configuration from there, expect additional plugins and modules there and create data and logs thre.

To ensure that Elasticsearch starts successfully, you need to create the home directory on the file system. You also need to create a subdirectory "config" and put the files "elasticsearch.yml" and "logging.yml" in there.

In my case, the Elasticsearch home directory is "/Users/dm/Projects/elasticsearch/data/2.x" (the "data" in there has nothing to do with the ES data directory. I just call it this, because I have all data related stuff in there and in "/Users/dm/Projects/elasticsearch/src/2.x" I store the ES source code...).

If you run tree in your ES home directory, you should see this:

dm@io:data/2.x $ pwd
/Users/dm/Projects/elasticsearch/data/2.x
dm@io:data/2.x $ tree
.
└── config
    ├── elasticsearch.yml
    └── logging.yml

1 directory, 2 files

Elasticsearch will create all other directories automatically.

Daniel

Did you have to add any libraries to the classpath when running? I keep getting the JarHell Error. Have you ever heard of that? To get around this I've been just adding the elasticsearch/lib(from the prebuilt project). I'm trying to debug some of the ES code, but it looks like I'm just stepping into those libs that I added. I'm unable to make a change, run it locally and see the changes.

Hi @JonathanAaron,

do you have Elasticsearch embedded in your project? If yes, then you should set it up as a separate project in Intellij.

Just clone the Elasticsearch repo and checkout the branch that you need, e.g. 2.4 (you can also checkout a release tag). Then either import the project into Intellij or run mvn idea:idea on the command line.

On JDK 8 I get a JarHell error indeed:

Exception in thread "main" java.lang.IllegalStateException: jar hell!
class: jdk.packager.services.UserJvmOptionsService
jar1: /Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home/lib/ant-javafx.jar
jar2: /Library/Java/JavaVirtualMachines/jdk1.8.0_112.jdk/Contents/Home/lib/packager.jar
	at org.elasticsearch.bootstrap.JarHell.checkClass(JarHell.java:282)
	at org.elasticsearch.bootstrap.JarHell.checkJarHell(JarHell.java:186)
	at org.elasticsearch.bootstrap.JarHell.checkJarHell(JarHell.java:87)
	at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:180)
	at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:286)
	at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:45)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Refer to the log for complete error details.

So both Jars contain the same class. When I remove ant-javafx.jar via File -> Project Structure -> Platform Settings -> SDKs -> 1.8, then Elasticsearch will boot.

You can start Elasticsearch in Intellij in debug mode and connect your application to it.

Daniel

Thanks Daniel! That did get me past that JarHell Error and came up with another one, probably since I'm using 5.x and you're on 2.x.

I did find a work around. 5.0 uses gradle. So I found in some documentation under the [testing ascii doc] (https://github.com/elastic/elasticsearch/blob/master/TESTING.asciidoc) that you can run elasticsearch doing gradle run --debug-jvm in terminal and running a Remote in Run/Debug Configurations:

Hi @JonathanAaron,

ah, I see. I assumed that you wanted to try this on 2.x. Great that it works now with remote debugging.

Daniel

lol, I was desperate, I didn't care if people was running 0.x, 2.x :slight_smile: I also totally forgot how to remove jars from the classpath in intellij, so I'm glad you reminded me.