Create deployable JRuby JAR file? - jruby

I need to compile my JRuby application into a standalone JAR file. How can I do this?

Warbler 1.3.0 or newer can also be used to make an executable JAR file.
Quick instructions. Make sure you're using JRuby`s gem, etc. here:
$ gem install warbler
$ mkdir bin
$ cat <<EOF > bin/myapp
#!/usr/bin/env jruby
puts "Hello World"
EOF
$ chmod a+x bin/myapp
$ warble jar
You should now have a myapp.jar file in the current directory. According to the README you just add any libraries you need in the lib directory and use either (or both) a .gemspec or Gemfile to control any other gems that need to be put into the .jar file.

Check out rawr. It is a gem that packages your application in a JAR file.
If you want to compile for source code protection you must produce a .class file for each script in your project (as described here) and then replace the contents of the original script with
require 'the_corresponding_class_file'
for each of the compiled scripts.

JRuby 1.6 improved this. There's now a section of the wiki - StandaloneJarsAndClasses - that talks about how to use jrubyc to generate .class files and standalone jars.
With the new compiler, you can build .class files like this example from the wiki:
james#gealach:/tmp/fnx$ cat my_foo.rb
class Foo
def bar(a, b)
puts a + b
end
end
james#gealach:/tmp/fnx$ ~/jruby/bin/jrubyc --javac my_foo.rb
Generating Java class Foo to C:/cygwin/tmp/fnx/Foo.java
javac -d C:/cygwin/tmp/fnx -cp C:/cygwin/home/james/jruby/lib/jruby.jar;. C:/cygwin/tmp/fnx/Foo.java
james#gealach:/tmp/fnx$ ls
Foo.class Foo.java my_foo.rb
james#gealach:/tmp/fnx$ javap.exe Foo
Compiled from "Foo.java"
public class Foo extends org.jruby.RubyObject{
public static org.jruby.runtime.builtin.IRubyObject __allocate__(org.jruby.Ruby, org.jruby.RubyClass);
public Foo();
public java.lang.Object bar(java.lang.Object, java.lang.Object);
static {};
}
And you can set the entrypoint for a jar to org.jruby.JarBootstrapMain and add a jar-bootstrap.rb file.

To just run a script:
The JRuby site has a runnable JAR file that you can get at the JRuby download page. You want the JRuby complete JAR file. You can then run your application by doing
java -jar jruby-complete-1.4.0.jar <script>
I believe you can also build the same JAR file from source. In the downloaded source do
ant --projecthelp
To embed a Ruby script completely into a JAR file: Embedding JRuby in Java is a good starting point. You will probably want to unjar the jruby-complete.jar file, add your Java main class that either has the a callout to a JRuby script or has the Ruby code embedded in it, replace the manifest's main class to point to your new entry point, and jar it back up.

Related

How do I make Octave see installed Shogun libraries in Ubuntu 16.04?

I copied the commands (from these instructions: http://www.shogun-toolbox.org/install#ubuntu) into the terminal and they seem to have worked, but there is no documentation on how to make Octave find the libraries. I have tried modshogun and init_shogun but Octave cannot find them. I do have the libraries in usr/lib, and I have put that directory on PATH. I have even set usr/lib as my working directory in Octave and that did not help. As far as I have found, there is no Shogun documentation on what to do at this point.
I have also tried compiling Shogun from source, but configure couldn't find GCC. Apparently, this is a known problem with newer versions of GCC. I decided to ask for help with the former method because at least I have the libraries with that.
Edit: I am following the instructions here http://www.shogun-toolbox.org/install#manual-basics
When i do cd build and then "cmake -DINTERFACE_OCTAVE=ON" it tells me there is no cmakelists.txt. There is one in in the above folder, but when I go to that directory and do "cmake -DINTERFACE_OCTAVE=ON" again, it tells me "Shogun can only be built with GPL codes if the source files are in /home/derose/shogun/src/shogun/src/gpl. Please download or disable with LICENSE_GPL_SHOGUN=OFF."
However, when I add -LICENSE_GPL_SHOGUN=OFF as an option, i get the error "CMake Error: The source directory "/home/derose/shogun/src/shogun/-LICENSE_GPL_SHOGUN=OFF" does not exist."
You've linked to the Ubuntu install instructions. From there
These currently do contain the C++ library and Python bindings..
No word that this would include the GNU Octave binding. See below on the same page:
The native C++ interface is always included. The cmake options for building interfaces are -DINTERFACE_PYTHON=ON -DINTERFACE_R .. etc. For example, replace the cmake step above by cmake -DINTERFACE_PYTHON=ON...
So you have to grab the source and fire up cmake with something like -DINTERFACE_OCTAVE=ON
Steps to build the bleeding edge of shogun (the github repo) and the Octave interface:
git clone https://github.com/shogun-toolbox/shogun && cd shogun
git submodule update --init
mkdir build && cd build
cmake .. -DINTERFACE_OCTAVE=ON
make -j4

Failed sqljdbc4 connection when in executable JAR

I have a GUI application that connects to a SQL Server 2008 database using sqljdbc4.jar. If I run this from the command line it works great.
However, once I wrap it into an executable JAR, I get a "No suitable driver found for jdbc:sqlserver://myServer:1433;databaseName=myDb" error. I know that my Windows system CLASSPATH is ignored once it's in a JAR, but I can't figure out include the sqljdbc4.jar within the executable JAR and get a portable application.
My most recent attempt to fix this is as follows:
My MANIFEST.MF file:
Manifest-Version: 1.0
Class-Path: lib/sqljdbc4.jar
Created-By: 1.7.0_11 (Oracle Corporation)
Main-Class: MyApp
The command I use to compile the JAR:
jar cmf MANIFEST.MF MyApp.jar MyApp.class help.html lib
My directory structure is below. I am running the jar command inside the MyApp directory.
+ MyApp
+ lib
- sqljdbc.jar
- help.html
- MANIFEST.MF
- MyApp.class
- MyApp.java
When I create the JAR and run it inside the MyApp directory, it works fine. As soon as I pull it out of the directory, I get the error. How can I make the JAR access the sqljdbc.jar that is available internally?
Thanks in advance.
When you have an executable jar MyApp.jar with entry in the manifest.mf:
Class-Path: lib/sqljdbc4.jar
This means that the jar has an external dependency to sqljdbc4.jar in the (relative) folder lib. It is not included in the jar you created! So you need to make sure that there is a lib-folder containing sqljdbc4.jar relative to that jar when you execute it. So the folder structure when executing needs to be;
+ (a folder)
+ lib
- sqljdbc.jar
- MyApp.jar

Equivalent to libmysqldev FreeBSD

I have compiled a program in fedora using the mysql dev library (include mysql.h in header file). I need to compile in on FreeBSD. I do not want to download from source and compile but rather would like to download from ports or something equivalent to facilitate removing it if need be.
Does anyone know the equivalent of the libmysql-dev in FreeBSD. I have not found it in ports?
You do not need a special package for this. The standard mysql client package/port already includes the libraries and the header files you will need. The header files will end up, by
default, in /usr/local/include/mysql/ directory, while
the libraries will go to /usr/local/lib/mysql/.
So - just install a client from ports or packages, and you are set.
mysql.h is included in the mysql-client-xx port.
assuming you have a configure script or makefile you should set the LDFLAGS and CPPFLAGS environment variables to:
export LDFLAGS='-l /usr/local/lib'
export CPPFLAGS='-I /usr/local/include'
(or setenv, if using csh)
then ./configure and make as normal.
if you are compiling from the command line "gcc -o myprog mysource.c" just add the -I and -l options to your command and it should compile fine.
try: find /usr/local -iname 'mysql' to see files (headers, shared objects and binaries) you in fact have installed on your system.

Does Clojure have a configuration file similar to ~/.clisprc.lisp?

On my platform, I need to add (set! *compile-path* (str *compile-path* ":.")) in order for (compile) to find my scripts. I'd prefer not to have to type that every time I want to compile something.
The easiest way to handle setting your "compile path" in Clojure is to use a build tool like Leiningen or Cake to manage your project. Using these tools, you get an idiomatic project structure, all your source code automatically on the compile/class path, and nice command-line tools to handle dependency retrieval, running unit tests and building your projects.
Here are some of the basic command-line tasks defined by Leiningen, and thus available to you in any project:
classpath Show the classpath of the current project.
clean Remove compiled artifacts and jars from project.
compile Compile Clojure source into .class files.
deps Download all dependencies and place them in the :library-path.
help Display a list of tasks or help for a given task.
install Install the current project or download the project specified.
interactive Enter interactive shell for calling tasks without relaunching JVM.
jar Package up all the project's files into a jar file.
javac Compile Java source files.
new Create a new project skeleton.
plugin Manage user-level plugins.
pom Write a pom.xml file to disk for Maven interop.
repl Start a repl session either with the current project or standalone.
run Run a -main function with optional command-line arguments.
swank Launch swank server for Emacs to connect.
test Run the project's tests.
test! Run a project's tests after cleaning and fetching dependencies.
uberjar Package up all the project's files and dependencies into a jar file.
So you start a new project by running lein new <name of project>, which generates a standard directory structure for a Clojure project. After you've written your code, you can run lein compile to simply compile your Clojure source, or you can go right to lein jar to package your code as a Jar file. For an executable jar that includes the Clojure language and all dependencies necessary to run your program, use lein uberjar instead.
If you don't use these tools, then you need to manage the classpath manually, to include where you store your dependency jars and where your source code lives. I highly recommend using one of the above-mentioned build tools.
You can specify -i when running Clojure to have it evaluate a file when starting up.
Below is the script I use to run Clojure as an example:
#!/bin/bash
# GUI mode
if [ "$1" != "--no-fork" ]; then
gnome-terminal -t Clojure -x $0 --no-fork $* &
exit
fi
shift
breakchars="(){}[],^%$##\"\";:''|\\"
if [ -f project.clj ]; then
lein repl
else
rlwrap --remember -c -b "$breakchars" \
java -Djava.ext.dirs=$HOME/.clojure clojure.main \
-i $HOME/.clojurerc --repl
fi
Leiningen will load ~/.lein/init.clj every time it launches. In addition, you can add a :repl-init key to your project.clj files to have that namespace loaded in each repl. Clojure is really not meant to be used standalone without any supporting tools, so calling (compile [...]) on your own is almost never the right answer.
In Clojure this is managed at the Java level (classpath etc) rather than having a .rc file. When I first started programming in Clojure I had a bash script that I would run, but now I use Leiningen.

Rawr-created jar throws ClassDefNotFoundError

I am trying to use Rawr to turn a simple 3-file Ruby project into an executable jar to be run on Ubuntu. I ran jruby -S rawr install to create the Rakefile and set up the directory layout. All my .rb files are under the src directory at the root of the project. When I run jruby -S rake rawr:jar, it compiles all the Ruby files and creates the jar just fine. It then places it in my deploy directory along with lib/java/jruby-complete.jar.
I try executing the jar from inside deploy with java -jar hg_analyzer.jar. However, when I do this I am greeted with this error:
Exception in thread "main" <script>:1:in `require': Linkage error loading compiled script; you may need to recompile 'file:/home/marcw/Documents/hg_analyzer/deploy/jar/hg_analyzer.jar!/main.class': java.lang.NoClassDefFoundError: org/jruby/ast/executable/AbstractScript$RuntimeCache (LoadError)
from <script>:1
...internal jruby stack elided...
from Kernel.require(<script>:1)
from (unknown).(unknown)(:1)
I've also tried running it as java -cp lib/java/** -jar hg_analyzer.jar, but to no avail. The Class-Path entry in the jar's manifest file looks okay as well. main.rb is the main Ruby file.
Running java with the -verbose option shows that it is finding the JRuby jar and is loading it from the expected location.
Any ideas?
Figured it out! The problem was that Rawr was downloading version 1.5 of the JRuby runtime and I have version 1.4 of the compiler on my system.