La-comadreja
La-comadreja

Reputation: 5755

run Scala code in package (LinkedIn Norbert)

I am trying to run a Scala object called NorbertClusterClientMain which is in package com.linkedin.norbert.cluster. The source code for it is in folder examples/src/main/scala of rhavyn's open-source branch of LinkedIn Norbert, and I am working on a Linux command line.

Although I've been told that running Scala code in a package is like running Java in a package, I am in examples/src/main/scala but cannot use this command:

 $ scala com.linkedin.norbert.cluster.NorbertClusterClientMain

I am getting "No such file or class on classpath", even though the file exists.

I was successfully able to compile Norbert with

$ mvn clean -DskipTests install

How can I run the NorbertClusterClientMain? Please let me know. I appreciate your help.

Upvotes: 1

Views: 360

Answers (5)

tuxdna
tuxdna

Reputation: 8487

Norbert uses a Scala 2.7 so directly using Scala from CLI may not work for you. Therefore find all the dependency jar using Maven and use it.

This is how I did it.

First, check out the code:

$ git clone https://github.com/rhavyn/norbert
$ cd norbert/

Build and install the dependencies in local repository first:

$ mvn clean install

Setup a classpath variable which we will use later for examples/ folder:

$ cd examples/
$ export CP=$(mvn dependency:build-classpath | grep -A1 'Dependencies classpath:' | tail -1)

Run server from examples/ folder:

$ java -cp $CP:target/classes com.linkedin.norbert.network.javaapi.NorbertJavaNetworkServerMain arg0 arg1
log4j:ERROR Could not find value for key log4j.appender.R
log4j:ERROR Could not instantiate appender named "R".
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
    at com.linkedin.norbert.network.javaapi.NorbertJavaNetworkServerMain.main(NorbertJavaNetworkServerMain.java:33)

Run client from examples/ folder:

$ java -cp $CP:target/classes com.linkedin.norbert.cluster.NorbertClusterClientMain localhost 1011
log4j:ERROR Could not find value for key log4j.appender.R
log4j:ERROR Could not instantiate appender named "R".
> h2013-12-20 13:59:44,323 - WARN  [pool-1-thread-2-SendThread(0.0.3.243:2181):ClientCnxn$SendThread@1120] - Session 0x0 for server null, unexpected error, closing socket connection and attempting reconnect
java.net.SocketException: Invalid argument
    at sun.nio.ch.Net.connect0(Native Method)
    at sun.nio.ch.Net.connect(Net.java:364)
    at sun.nio.ch.Net.connect(Net.java:356)
    at sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:623)
    at org.apache.zookeeper.ClientCnxn$SendThread.startConnect(ClientCnxn.java:1009)
    at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1036)
2013-12-20 13:59:46,192 - WARN  [pool-1-thread-2-SendThread(0.0.3.243:2181):ClientCnxn$SendThread@1120] - Session 0x0 for server null, unexpected error, closing socket connection and attempting reconnect
java.net.SocketException: Invalid argument
    at sun.nio.ch.Net.connect0(Native Method)
    at sun.nio.ch.Net.connect(Net.java:364)
    at sun.nio.ch.Net.connect(Net.java:356)
    at sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:623)
    at org.apache.zookeeper.ClientCnxn$SendThread.startConnect(ClientCnxn.java:1009)
    at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1036)

Thats it. Now all you have to do is setup the required services like Zookeper etc.

Upvotes: 0

som-snytt
som-snytt

Reputation: 39577

I don't anything about the code base, but that class is in the examples subproject.

This shows that it loads normally. (I haven't configured anything, because I don't know anything about the code base.)

apm@mara:~/clones/norbert$ cd examples
/home/apm/clones/norbert/examples
apm@mara:~/clones/norbert/examples$ ls
pom.xml  src  target
apm@mara:~/clones/norbert/examples$ mvn exec:java -Dexec.mainClass=com.linkedin.norbert.cluster.NorbertClusterClientMain
[INFO] Scanning for projects...
<snip...>
[INFO] --- exec-maven-plugin:1.2.1:java (default-cli) @ norbert-examples ---
[WARNING] 
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:297)
    at java.lang.Thread.run(Thread.java:724)
Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
    at com.linkedin.norbert.cluster.NorbertClusterClientMain$.main(NorbertClusterClientMain.scala:22)
    at com.linkedin.norbert.cluster.NorbertClusterClientMain.main(NorbertClusterClientMain.scala)
    ... 6 more

Here's how to add args:

https://stackoverflow.com/a/9846103/1296806

I don't use maven much anymore.

Edit: I don't use Scala 2.7.7 much anymore either.

Upvotes: 1

Glen Best
Glen Best

Reputation: 23105

Your mvn script produced JAR and class files within target directory:

./target/com/.../<someClassName1>.class
./target/com/.../<someClassName2>.class
... etc
./target/<someJarName1>.jar
./target/<someJarName2>.jar
... etc

Great! Now do the same thing that you must do for java; include in your classpath:

  • the target base directory (this "picks up" all class files in directory hierarchy beneath target)
  • each jar file (this "picks up" all class files in directory hierarchy within each JAR)

    scala -cp target:target/<someJarName1>.jar:target/<someJarName2>.jar:... etc ./com.linkedin.norbert.cluster.NorbertClusterClientMain
    

Here -cp (or equivalently, CLASSPATH environment variable) is the java classpath and so has the same syntax and rules as java.

BTW: "sbt" is a standard, powerful, usable way to build scala projects. It uses Ivy to "pull" dependencies from code repositories (i.e. mvn++). The best way to get started with it is to download sbt example projects, search for "sbt tutorial" blogs and read the sbt docs. :)

Upvotes: 1

Daniel C. Sobral
Daniel C. Sobral

Reputation: 297165

It is the same. So, in this case, it is looking for this file:

./com/linkedin/norbert/cluster/NorbertClusterClientMain.class

This is how Java works, and since "running" a Scala program is just running java passing the Scala library in the classpath, it has to be the same.

How did you compile it, by the way? Nevermind, saw your comment. At the directory you ran mvn, you should probably be able to run it like this:

scala -cp target com.linkedin.norbert.cluster.NorbertClusterClientMain

Failing that, find the class file, and pass the directory where com/ is to the classpath.

Upvotes: 1

Konstantin
Konstantin

Reputation: 3294

try $ scala ./com.linkedin.norbert.cluster.NorbertClusterClientMain or -cp .

you "current directory" might not be in classpath

Upvotes: 0

Related Questions