Java out of process component for multithreading - java

I recently started to work in a java project and i am from vb background.
Is there any equivalent of VB6 ActiveX Exe development in java which enables a component to run in its own process and useful in multi threading requirements?

I think the only way is to start another JVM from the JVM you're currently in.
But I don't really see why someone would want to do that in a real-world scenario.
Apart from that all multi-threaded execution happens in the same JVM you're in.

Java has API for working with external processes.
https://docs.oracle.com/javase/7/docs/api/java/lang/Process.html
You can use ProcessBuilder to create it.

Related

How to create a GUI in Java that runs another language in the background?

So for a hackathon I want to use this domain-specific language, Cryptol, in order to create an encryption system. However, I want a GUI for said system.
I have no idea how I could write a GUI in Java that run Cryptol in the background. Is this possible?
Thanks
This is a very broad question, however, let me take a stabe.
There are multiple ways to create a GUI in Java (JavaFX, JSF, JSP, Swing to name a few). Without any experience, I would say JavaFX is the easiest to get started with. But that is completely opinion based of course.
When there is no standard API (library) to interact with Cryptol, you should create a rudimentary API for yourself. How? That completely depends on which functionalities from Cryptol you need and how Cryptol exposes it's interfaces.
You can use the ProcessBuilder class in java to execute programs through the command line. Essentially, ProcessBuilder allows you to execute programs as if you were to execute them in the command line by passing a string to the ProcessBuilder constructor. You can then read the response from ProcessBuilder to view the output of your other programs. Here is the documentation for Process Builder -> http://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html

Is there any Java tool to communicate with an external CLI?

I need a way to communicate with an external tool which has a Command Line Interface from my Java application. Is there any handy tool/lib to make it less painful? Believe me, I've googled enough. Thank you!
You can use Runtime.exec() for executing external commands of the operating system. Be aware, though, of the gotchas of using it - make sure to read this article first.
If that's too simplistic for your needs, take a look at ProcessBuilder. It's available since Java 1.5, from the release notes:
The new ProcessBuilder class provides a more convenient way to invoke subprocesses than does Runtime.exec. In particular, ProcessBuilder makes it easy to start a subprocess with a modified process environment (that is, one based on the parent's process environment, but with a few changes).
I've ended up using the process handler (more specifically, the com.intellij.execution.process.OSProcessHandler) from Intellij IDEA platform API + writing my own small command-based framework where each command knowns how to execute itself (write) and parse the response from a process (read).

netlogo in java

I need to create a large library to determine turtles' behaviour and need an interface to show the results after they completed their behaviours. Is it possible to run Netlogo in Netbeans ? If possible , does it create problems after a while such as limited reaching to codes ,slows down or anything else?
Actually, you can use the "HeadlessWorkspace" functionality to run NetLogo programs from within a Java program (or unit test). So, using this, you can definitely step through responses from a model within a NetBeans debugging session (e.g. querying reporters).
Please see the following page for the projects' description of this functionality:
https://github.com/NetLogo/NetLogo/wiki/Controlling-API
NetLogo is a Java program which implements both an IDE (Integrated Development Environment) for the NetLogo programming language, as well as a NetLogo interpreter. NetBeans is an IDE for Java (and other languages, but not NetLogo).
So, no, you cannot run NetLogo programs in Netbeans. Netlogo programs only run in the Netlogo IDE.
You are also asking about speed. Yes, a NetLogo program probably runs slower than an equivalent program written in Java. It depends on the specific program.

Bullet-proof groovy script embedding

I'm working on a server app that may be extended by user-supplied Groovy scripts. It's evident that I want to make sure these scripts run in a very tight sandbox where they cannot disrupt the core application code or consume too much resources to overload the server.
I have studied various possibilities and the final solution may be a combination of these:
Run the script within a very restricted security manager. The script is run within a no permission SecurityManager. Additional permissions have to be declared (like Android).
Launch a new JVM. Create a ScriptProcess wrapper around Runtime.exec and spawning a new JVM with a security manager, limited heap, etc. Because we launch a full-blown process, we might get more control on monitor bad behaving ones? The cost in resource would be dire though... An alternative would be to use Ant here, but would it be scalable?
Java Monitor API In Java 6 there is a package with monitoring capacity. We could monitor threads and maybe detect infinite loops and memory consumption. Anyone used this?
These are what I have in mind today. What would be the best way to make sure these scripts behave correctly and still keep a certain scalability and performance?
An additional possibility is using Groovy 1.8 compilation customizers on the GroovyShell that runs the embedded scripts. You can pre-import classes and methods, restrict use of the Groovy AST, and pre-apply an AST transformation, such as #ThreadInterrupt, #TimedInterrupt, or #ConditionalInterrupt. Details at:
http://www.jroller.com/melix/entry/customizing_groovy_compilation_process
You should have a look at the project groovy-sandbox from kohsuke.
Have also a look to his blog post here on this topic and what is solution is addressing: sandboxing, but performance drawback.
Also have a look at the java-sandbox project and the accompanying blog post http://blog.datenwerke.net/2013/06/sandboxing-groovy-with-java-sandbox.html.

How to use multiprocessing with Java - NOT multithreading

I've been looking for an api for simple java-based multiprocessing and couldn't find anything.
I have legacy code which I need to integrate into my java application. Since this legacy (native) code crashes sometimes, the whole jvm crashes with it. So what I want to do, is to run this code and its adapter in a different process (not thread!).
There's the ProcessBuilder in the newer java jdks, it lets you start a new process and gives you a In/Outputstream; so solving my problem is possible by hand. In order to do so, you have to find the path to your jvm and start it up with the for your process. Then you have to use the stream to communicate.
Is there something which takes over that job? Or do I really have to do it by hand?
AFAIK, people usually do this by hand.
The problem is that there's no truly portable way of doing it ... what with the difficulty (impossibility) of identifying where the currently running JVM was launched from, and the fact that JVM command-line options are vendor, version and (potentially) platform specific.
The simple solution is just to put the JVM path and options for launching the child JVM into some configuration file.
You can use the -Dprocess.name=$1 and let your main class take in some command line args. You can invoke by calling something like this:
java -cp $CLASSPATH $VM_ARGS $MAIN_CLASS
and your VM_ARGS can be defined something as
VM_ARGS=" -Dprocess.name=$1"
FWIW, I wrote a replacement class to take care of a lot of the I/O stream redirection nastiness, at david.tribble.com/src/java/tribble/util/RuntimeExec.java
You are looking for the technology called Java RMI (Remolt Method Invocation).
This allows one JVM to call a method in another JVM. This can be on the same machine or over a network.

Categories

Resources