Replacing classpath argument with CLASSPATH environment variable - classpath

Requirements:
Command-line only, i.e. no IDE's
Do not want to use classpath argument
OS: OS X v10.6.8
Steps:
Setup CLASSPATH environment variable: export CLASSPATH="/path-jar-files-are-at/*"
Compile my java app: javac MyApp.java
Launch my java app: java MyApp
After launching, the following error appears:
Exception in thread "main" java.lang.NoClassDefFoundError: MyApp
Caused by: java.lang.ClassNotFoundException: MyApp
Using classpath argument doesn't have this problem
Compile my java app: javac -classpath /path-jar-files-are-at/* MyApp.java
Launch my java app: java -classpath /path-jar-files-are-at/* MyApp
Any ideas?

The CLASSPATH should not contain the trailing "/*" - that is not going to give you a valid classpath. The shell is expanding the /* and separating the list with whitespace/newlines - rather than the ":" character.
If you still really want it to work this (dynamic) way, try something like this:
export CP=`ls /path-jar-files-are-at/*`; CP=`echo $CP | sed 's/\.jar\s/.jar:/g'`; CP="$CP:/path-jar-files-are-at"

Related

How to compile Kotlin to include jar of Java with command line?

I want to include jar of Java to Kotlin. I tried like below, but I had error.
javac -encoding utf-8 javasorce/test/JavaTestClass.java
jar cvf javasorce/test/JavaTestClass.jar javasorce/test/JavaTestClass.class
kotlinc kotlin/CallJavaTestClass.kt -cp javasorce/test/JavaTestClass.jar -include-runtime -d kotlin/CallJavaTestClass.jar
java -jar kotlin/CallJavaTestClass.jar
The error is:
Exception in thread "main" java.lang.NoClassDefFoundError:
javasorce/test/JavaTestClass at CallJavaTestClassKt.main(CallJavaTestClass.kt:5)
Caused by: java.lang.ClassNotFoundException: javasorce.test.JavaTestClass
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
I am using like below directories:
root
|-javasorce
| |-test
| |-JavaTestClass.java
|-kotlin
|-CallJavaTestClass.kt
Please tell me if there is solution.
In addition to compiling the source code with the Java library on the classpath, you need to run the program with the same library on the classpath: if a class is there at compile time, you need it on the classpath at run time as well to be able to use it.
The correct way of running an application which has its classes scattered across several JARs is to pass those JARs as the classpath to java and to additionally specify the class that has the main function:
java CallJavaTestClassKt -cp kotlin/CallJavaTestClass.jar:javasorce/test/JavaTestClass.jar
The command above assumes that you placed the main function on top level of CallJavaTestClass.kt (in this case, the class name is formed by the file name with .kt replaced by Kt), and it has no package ... declaration. If you have a package, prepend it to the class name as com.example.FileNameKt. If you declare main in an object or a companion object, use the class name or the object name (without Kt) instead of CallJavaTestClassKt.
See also: How to run Kotlin class from the command line?

Scala package ith SBT - Can't find " .../immutalbe/Map"

I've created a simple app that generates PageView for later Spark tasks.
I've only one scala file taht use a simple MAP
When created a package with SBT I run my class with command:
java -cp .\target\scala-2.10\pageviewstream_2.10-1.0.0.jar "clickstream.PageViewGenerator"
but I receive this error:
Exception in thread "main" java.lang.NoClassDefFoundError: scala/collection/immutable/Map
What I am doing wrong?
Many thanks in advance
Roberto
To run it correctly you need to add Scala runtime library into your class path:
java -cp $SCALA_HOME/lib/scala-library.jar;.\target\scala-2.10\pageviewstream_2.10-1.0.0.jar "clickstream.PageViewGenerator"
But .. you can run your application also as:
scala -classpath .\target\scala-2.10\pageviewstream_2.10-1.0.0.jar "clickstream.PageViewGenerator"
when you have scala already in PATH
or use directly sbt as:
sbt "runMain clickstream.PageViewGenerator"
when clickstream.PageViewGenerator is you only application it is enough to run:
sbt run
or when you are in sbt interactive mode just type:
> runMain clickstream.PageViewGenerator
or when it is only application in your project it is enough to run:
> run

Run junit4 test from cmd

I tried to run junit4 test case from command line using:
java -cp junit-4.8.1.jar;test\Dijkstra;test\Dijkstra\bin org.junit.runner.JUnitCore Data0PathTest00
but I got the following error:
java.lang.NoClassDefFoundError: graph/shortestgraphpath;
while the test case is working without any problems in eclipse.
Hint: in eclipse, shortestgraphpath was added in Referenced Libraries.
You need to the jar file containing shortestgraphpath to java class path.
java -cp junit-4.8.1.jar;test\Dijkstra; test\Dijkstra\bin org.junit.runner.JUnitCore Data0PathTest00
The class path is the value that you pass to java with -cp so in your question you just supply junitand your compiled classes.
Try updating it with the jar file with the missing class.
java -cp junit-4.8.1.jar;<path to jar file>;test\Dijkstra;test\Dijkstra\bin org.junit.runner.JUnitCore Data0PathTest00
You might have to add additional jar files as well. I recommend that you take a look at some build tool to help you build and run your java applications for example Maven, Gradle, Buildr.

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.
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.
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. :)
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.
try $ scala ./com.linkedin.norbert.cluster.NorbertClusterClientMain
or -cp .
you "current directory" might not be in classpath
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.

How o run a NetBeans-built Scala application jar from command line outside IDE?

A program of mine (written in Scala 2.8) works fine when launched by means of NetBeans IDE. But when I try to run it from outside, with "java- jar", it says "Exception in thread "main" java.lang.NoClassDefFoundError: scala/ScalaObject...". Putting all the libraries, incl. Scala runtime inside the same dir as the jar to be run doesn't help. If i try to run the jar with scala itself, it complains that it can't decode it as utf-8 (it expects a scala source rather than a jar, I suppose). So how do I run a Scala application at all?
UPDATE: For those who come here later having the same question I'd recommend to read comments under barjak's answer (including those latest ones hidden), the answer is there. VonC also gives some interesting links on the subject.
The -jar and -classpath of the java command are mutually exclusive : you can't do java -jar YourScalaProg.jar -classpath scala-library.jar
If you want to run your application with java -jar, then the full classpath must be specified in the Class-Path section of the jar's manifest.
You can run your application using only -classpath, like that : java -classpath YourScalaProg.jar:scala-library.jar your.package.MainClass.
Are you using scala-library.jar as described in Adventures with Scala blog post?
java -classpath scala-library.jar:. YourClass
or:
java -classpath scala-library.jar:yourApp.jar YourClass
Where YourClass is was your scalac compiled Scala code.
You will find the same scala-library.jar used in the SO question "Creating a jar file from a Scala file" (or in the blog post "the not so elegant way of creating an executable jar from scala code").