Attach a clojure / scala repl to a running JVM - java

I have a java web application running under tomcat in a Sun java 6 JVM. Is there a way to attach a scala or clojure REPL to the running JVM ?
Once the webapp is up and running, the context in which the calls are to be made is already setup in the running VM. Hence, this can be really helpful in invoking arbitrary java method calls for incremental, exploratory development and for debugging.

Copied over the answer from the other question as per your request:
liverepl: Connect a Clojure REPL to running Java or Clojure processes without any special setup
From the website:
Start a Clojure REPL connected to any running Java or Clojure process
without needing the process to be setup in any special way beforehand.
Now supports connecting to Tomcat web applications.
You can use the repl to run code, inspect variables, and
redefine Clojure functions.
https://github.com/djpowell/liverepl

There's no way to attach a REPL to an already-running JVM, so you need to think about the problem backwards...
Start the REPL on the server, either when the program is launched or in response to a inbound client connection (using e.g. Telnet).
There's a good example here: http://blog.homecloud.ca/2010/03/scripster-interactive-scala-repl-using.html
You could also host an Interpreter instance in your server, then expose a web interface. This is the approach taken by http://www.simplyscala.com/
If you're thinking of debugging, then you'd be better served by this answer: Drop into interpreter during arbitrary scala code location

You could use this:
https://github.com/wirde/swank-inject
Allows you attach a Clojure repl to a running java process (with remote debugging enabled).
You are also able to specify a number of classes (singletons) for which instances will be found and bound to a symbol available in the repl.

For Scala REPL connecting to other JVM, take a look at Scalive.

Related

How to create java interface for existing ruby project

my project has a significant and isolated part that was written in ruby(jruby compatible). It is a commandline application that we run it in the terminal and provide it with various option flags.
My client wants to use this tool but only willing to use it if it is wrapped in a java class. I went through a lot of trouble to convert the ruby code to java by using jrubyc --javac A.rb. Inspecting the converted .java file, it is calling a Ruby Runtime to execute the ruby script. Like this:
org.jruby.Ruby.getGlobalRuntime().executeScript(stringBuiltFromARubyFile, 'path')
My question is performance wise, is this better than just wrap the create a runnable jar, wrap it with a java class that takes certain parameters, and execute the jar via Runtime.getRuntime().exec("java -jar A.jar args") ?
The application A.rb uses multi-threading. I bring in ruby dependencies (gems) using jruby-gradle plugin.
What are some other options I can explore?
Thanks in advance.
The approach you are using likely won't work. You are launching a JVM within a ruby context; and what you've been asked for is launching some of your ruby code within a JVM context.
I would look at C to Ruby bindings, and then use a Java to C (JNI) interface to launch the required Ruby code from the C layer. If such a thing is not practical, as your Ruby is more of a standing service than a CLI process, I would then consider making a set of Java libraries to call the process through a network call.

How to make PHP Java Bridge works

I am using PHP 5.5 with Apache 2.4 and Java 8. Now I want to call the function within a Java JAR file. I search the internet about the PHP Java bridge. I have found a JavaBridge.Jar file for the SourceForge, but I don't know how to make it works. So how can I use PHP Java Bridge. I found by Google saying it may use PHP exec() to execute the Java function, however, this method seems need to open the JVM each time. So I am wondering whether it has a better method to call function in JAR.
You have to have the jvm running at some point if you want to execute your java function - you are correct that running the jvm many times is not ideal.
PHP's exec call simply runs an external program (resident on the server). If you write a complete Java application, your program could then call whatever function you want. This, of course, will result in the jvm having to start for each instance of the program (someone can correct me if there will be pooling, but I suspect not).
A better solution would be to work out a way to keep a single java program running indefinitely, and communicate between your PHP code and that one instance of your java program (which does the function calling). A pretty straight-forward way to do this would be with a Socket (PHP) that connects to another Socket (Java "ServerSocket"). Build a java application as a 'server' that accepts requests from your PHP 'client'.
Google has been very helpful to me in the past in getting started with network communication with java/etc.
The library that you mentioned looks like it does something like this for you, but it looks like you will need to have some sort of java application available to connect to - not just a raw jar.

Java Projects on Django?

ok, So i searched net for the possible implementation but all that I managed to find is Django projects implementation on Java platform through Jython. But I want to do the reverse, i.e. implement/integrate java project ( which in my case is SAIKU server ) on Django platform.
The question being, is it possible, and if yes, then kindly point me to the solution.
Thanking in advance =)
For your specific requirement, I would suggest using RESTFul API to access the Saiku Server.
However if you need to run Java Classes from Python.
Here are the options available for you:
JCC -- a C++ code generator for calling Java from C++/Python. It produces producing Python extensions which communicate via JNI with a Java virtual machine. As it implies, this would require compilations of every possible call. However this project is backbone of PyLucene project.
CodeMesh. C++ code generator for Java.
Py4J Python programs running in a Python interpreter to dynamically access Java objects in a Java Virtual Machine.
JPype allow python programs full access to java class libraries. It is done through interfacing at the native level in both Virtual Machines. However there are no recent development in this front.
In general, having an loosly coupled integration through REST or RCP would be easy to maintain than tightly coupled JNI based implementation.
There's no way to run Java within the Python runtime (which is what it sounds like you want). There are Java to Python "translators" available, but they're terrible. Honestly, if you need a Java server and Django to sit inside the same process for some reason, Jython is the way to go.
There are lots of options outside of that though, off the top of my head:
Implement Python bindings for your server (See PyLucene for an example)
Implement a socket server within your Java server that Python can talk to directly

CLI communication with already running Java application?

When starting a application, one can pass parameters to the application. But how can one pass parameters to a already running (Java) application / how can I handle such cases in my Java program?
In other words: How can I communicate from a .bat file / CLI processes with a already running Java application? Note that both things (CLI stuff and Java application) are my own applications and I can adapt the source code - I just don't know how ;-)
I prefer using socket for cross platform IPC, with help of Apache thrift . You can implement RPC method to use from CLI utility.

How do I log every time I start an application in Windows?

I'd like to log or record every time I start an application to gain insight into which applications I use most on my Windows system. I was thinking I could create an event in the event log and listen for it in a .Net program.
Questions:
Is this the best way to solve this problem?
If so, which .Net library should I use?
I am also open to using Java to solve this problem. Thanks!
In .NET, you could probably create a shell extension which you would register for EXE programs which would really just be a filter for the EXE extension. When your shell extension is called, you would execute the program (by invoking the old functionality) after you logged your information.
Note, however, that you can ONLY do this with .NET 4.0 or above, which is currently in beta. Because of the way that previous versions of the CLR worked, only one version was allowed to run in a process at a time (including explorer, the OS process).
.NET 4.0 introduces Side-by-Side (SxS) CLR instances within the same process, so it is safe to use it from .NET 4.0 on as a mechanism for shell extensions.
It will also require a good deal of COM interop, but it can be done.
In regards to LWoodyiii's comment asking if this can be done in older versions of .NET: Could it? Yes, it can be done, but the official decree from MS is that you shouldn't. The reason for this is because if someone else decides to run a shell extension, or interface with the OS in some way using .NET, and the version is different from the one that you are using, you run the risk of hosing the OS process.
maybe you can try to hook the CreateProcess API in system wide using unmanaged c++.
and in C# use .NET interop to handle events/notifies from you unmanaged hook module.
related links:
http://www.codeproject.com/KB/system/hooksys.aspx?msg=1322916
http://www.madshi.net/madCodeHookDescription.htm

Categories