Jython can import lib when run in a single myapp-with-dependencies.jar but not when jython is separate - classpath

I have an application which consists of some Java classes and some Python scripts; I use Jython from my Java code to invoke the Python scripts.
My application is built using Maven, and produces an ordinary .jar file containing the compiled Java classes, and the Python scripts as resources. I also use maven-assembly-plugin to generate a myapp-with-dependencies.jar that contains the same plus bundles in the contents of the Jython dependencies.
When I run my application using the with-dependencies jar:
java -classpath target/myapp-1.0.0-SNAPSHOT-jar-with-dependencies.jar com.example.myapp.Main
...it works as expected. But when I run it using two separate jars:
java -classpath "target/myapp-1.0.0-SNAPSHOT.jar:/Users/richard/.m2/repository/org/python/jython-standalone/2.5.3/jython-standalone-2.5.3.jar" com.example.myapp.Main
...it fails: when I ask Jython to execute "import mylib" I get an exception:
ImportError: No module named mylib
In both cases, the exact contents of the classpath should be completely identical, the only difference is that in the first case everything is in one jar, but in the second case it is split across two jars.
What could be causing this behaviour?

Related

how to run scala .jar with external jar files in terminal

I have my .jar built from scala classes and it has an external dependency with other.jar. Please suggest how should I run my jar files in terminal. The command I tried is
$scala my_scala.jar external.jar
It works same way as running java program. Try this
scala -classpath <your_scala_jar>:<external_jar> <package.MainClass>

How to specify a different main class when running a scala jar on the command line?

I have a scala project with 2 objects that extend App. I have specified one as the main class in build.sbt. I'm using assembly to build a fat jar.
How can I execute the non-default main class when running the jar on the command line? I.e. if com.example.app1 is specified as the main class in build.sbt, how could I run com.example.app2 from the command line using the jar (assuming that also extends App)?
This will be in a production environment where I won't have sbt.
You run the default main class (from the jar's manifest) like this:
java -jar assembly.jar
And you would run a different main class like this:
java -cp assembly.jar com.example.app2

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.

Scala REPL unable to import packge

I'm trying to import com.lambdaworks.crypto.SCryptUtil (from crypto) in the Scala REPL. I'm running the REPL from the Java directory containing com/lambdaworks/crypto.
The REPL can't find com.lambdaworks.crypto.SCryptUtil, but it can autocomplete up to com.lambdaworks.crypto but can't find anything after that.
When I used the REPL in the IntelliJ IDEA after including the package in my project, I was able to find the SCryptUtil class.
Am I missing some classpath parameters that are required for import?
The REPL won't compile the Java code for you—it's only autocompleting that far because it's aware of the directory structure, but once it gets to the crypto directory it won't find any class files.
You can see this more dramatically by moving up a directory and opening a new REPL—you'll be able to autocomplete import java.com.lambdaworks.crypto, even though that's obviously not a real package hierarchy.
In this case you can move to the project root, run mvn compile to compile the Java code, and then start the REPL like this (still in the project root):
scala -classpath target/classes
Now you can import com.lambdaworks.crypto.SCryptUtil.
This only works because the project doesn't have any runtime dependencies, though—in other cases you may need either to add other things to the classpath, to build a JAR with the dependencies baked in (e.g. with the Maven Assembly plugin), or to use the mvn scala:console goal of the Maven Scala plugin.

Is classpath, junit.jar and import org.junit statement in Eclipse independent?

Are classpath, junit.jar and import org.junit statement independent of each other in Eclipse?
After adding junit.jar on Windows 7 (environment) classpath I am not able to benefit from importing org.junit.*; statement in Eclipse for Java. Eclipse informs that the import org.junit cannot be resolved. Is it normal behaviour?
By using command line (cmd.exe) junit works fine:
java org.junit.runner.JUnitCore org.junit.tests.AllTests
However, within Eclipse I cannot use Junit classes.
The project build path is in charge in Eclipse. If junit.jar (of appropriate version) or the JUnit eclipse lib are on the build path, you can import junit classes. If not, not. The classpath environment variable is never a good idea.
Eclipse builds classpath to based upon what is called a 'build path' and invokes JVM with a -cp argument. JVM ignores CLASSPATH env variable if an explicit -cp jvm arg is passed to it so your Windows classpath setting is ignored.
Solution: set up your project build path correctly ie. add your jars there.