Running a Java program in a Perl/CGI script - java

I have a program that uses external libraries and code I've written in Java. However, I want to make it accessible via the web.
If I had full control over the webserver I was running it on, I would probably use Tomcat or JBoss, but I don't have such privileges at my school.
The servers I do have access to have Apache HTTP server with all the normal Linux goodies installed (think: Perl, PHP, etc.)
How would I write a Perl script that runs this Java program?
I've tried the basics such as "system java MyProgram" and "exec java MyProgram", but they don't seem to work.
I'd appreciate any help or insight on this. Thank you!

Process process;
try
{
process = Runtime.getRuntime().exec("cmd / c start c:\\Perl\\bin\\file.pl");
try to run like this as per your program.

When you run a CGI script, the environment is very limited, and this includes the PATH. Is it possible that your CGI script can't find the java command? Or maybe Perl simply is refusing to run the system command when in CGI mode (aka taint mode). See perldoc perlsec for more information.
Basically, you need to set PATH and then try running your system command with your java command.

Try this:
system('/full/path/to/java -cp full_class_path my.class.Name');

Related

How to run Java application on startup of Ubuntu Linux

I have a requirement where I need to develop application that reads TCP/IP Socket. I successfully made the program as Java program with No GUI means as soon as program runs it starts listening to the Socket and gets the response with the help of Netbeans IDE. Now as per my requirement i have to start execution of this program as soon as Linux system Booted.
Actually I am very novice in Java and Linux Platform, so have few doubts..
Is my Socket Program with no GUI is fine to be run as per my requirement.
How can I write script to run jar on Linux Boot up, I got to know. init.d is meant for this.
Ideally you should create a service wrapper for your java application and then make this service run on startup example here.
Use
sudo update-rc.d mytestserv defaults to run your service wrapper on startup on Ubuntu
So two things you'll need to do:
First create a small shell script to start your java program from a terminal. As you have packaged as a jar have a look at this, specifically the JAR Files as Applications section.
This may be sufficient: (although you'll want to use the full path to Java)
#!/bin/bash
java -jar path_to_jar_file
You should be able to run your script and successfully start your program.
Once you've got it starting from a script you can use standard linux tools to start the script. Either putting it in /etc/rc.local, or as you're using Ubuntu, use update-rc.d to start it on boot. See here for a very simple example of using update-rc.d
Hope this helps,
Will

Run .sh (from linux) scripts on windows using eclipse

I have a .sh scripts in Linux.
I want to create a form in Java (eclipse) on Windows
and execute those .sh files.
Is there any simple way to do this?
At first you need to be able to run those shell scripts on target computer.
So try Cygwin, MinGW or other Unix emulators.
If run successfully next step is to use Runtime.getRuntime().exec(command) for running script. You have to set up environment (run another command before) to make it working.
The gcc compiler on CDT Eclipse for Windows is working this way.
.sh is a a shell script, most likely the default shell for a linux system, i.e. bash, so no, it won't run on your Windows machine. You can try Cygwin shell but still there is no guarantee that it will run. You will have to rewrite it for Windows batch script or your some other scripting language.
As far as Java is concerned, you can write in Linux and the code is portable to any other machine supporting JavaVM.
No there isn't. Perhaps intalling Cygwin would be an option, but I would not recommend to go that way.
Or perhaps I misunderstood. If you like running those scripts on your linux-machine, you can trigger a remote execution easily by using the appropriate java commmands or libraries (eg: jssh or anything like that).

Wrapping a jar file in a Windows service

Have you had experience with running a jar file using a command line, wrapped in a Windows service?
I'm trying to find a way to run a jar file without being logged into the machine, and since it allows command shell, I was wondering if it's a good idea.
Thanks!
Original Post:
I'm trying to run Associated Press's Web Feeds Manager, which is basically a jar file that can be run when logged in by double clicking it.
I'd like to run the same file but without being logged in to the machine. In their manual (http://wfm.ap.org/admin/content/help/Running_Agent_on_a_Remote_Server.htm) they write how to do that, using a commandline parameter.
Basically I'd like the jar to run as a Windows service, regardless of who's logged in, but Googling it showed it was problematic.
Have you had experience with remotely running jar files? What are the pitfalls?
Thanks!
On a google search, I came across this article -
Running Jar Applications as a Windows Service
It mentions about open source Java Service Wrapper project from Tanukisoftware.org for accomplishing this task.
Note: I've not used this personally.
If you are not interested in having the service started/stopped at boot/shutdown, but you just want the program to be started manually and keep running after logout, here is what you do:
$ nohup java -jar foobar.jar > foobar.log 2>&1 &
which means: start my foobar.jar (java -jar) and keep it running after I logout (nohup) redirect stdout to foobar.log (>) and also the stderr (2>&1), and make it running in background (& at the end).
Instead, if you are interested in installing a "service" in your linux box, there are many options, depending on what distribution you are using.
The most common are upstart (for ubuntu) and System V init scripts (Redhat or others). Also cron can be used to start/stop services at startup/shutdown.
You can find an example of installing a java app (hudson) on an init system here, or doing the same thing with upstart. Or, as I said, cron could be an option.
On Windows, there is Java Service Wrapper. And not much more.
For windows Java Service Wrapper is a better choice
My favourite is the upstart on linux, but it is Ubuntu only.
On Windows I see many alternatives according to this forum.

Calling a Java program from a CGI script fails

I have a Python CGI script from which I am trying to call a Java program to perform a task. The Java program uses JExcelAPI. When I run the Python script from the browser, it fails with error messages that it can't find the class definitions for the classes from JExcelAPI. I suppose this happens because the Python CGI script is run under the apache user, and the apache user does not have the appropriate environment variables set (namely the CLASSPATH variable). I have tried calling the program with the -classpath /path/to/JExcelAPI switch, but that does not work either. Can you help me find the way to make the apache user aware of the JExcelAPI? Is there a way to set the CLASSPATH environment variable for the apache user?
Thanks
Several solutions come to mind :
Create a bash script which calls the java program. You can set all the variables you like and debug on the commandline, e.g. sudo -u apache /usr/local/bin/java-task-wrapper. This simplifies calling it from a cgi considerably and the overhead of bash is negligeable compared to spinning up a JVM.
Create a standalone executable jar with tools like uberjar. No more classpah issues as everything is contained : java -jar java-task-standalone.jar
exec java -cp /path/to/JExcelAPI:/my/program/classes com.acme.MainClass
There is usually a variant of exec which takes an additional array or hashmap to add environment variables.
Some notes:
setting the CLASSPATH variable globally is not done anymore because it leads to many conflicts. In a wrapper script it is Ok as possibilities to clash is reduced.
JVM's take a long time to start and the execution will be slow since the JIT gets no chance to do its magic. Running your script in a lightweight webserver like jetty or winstone or listening on a socket will eliminate the startup cost and enabe the JIT to make things fast.

How i run cygwin using java program?

I have installed cygwin on window to start crawling. It work well.
Now i want to run cygwin and run a crawl program at starting of cygwin using java program code.
If you provide some code for it ,it will be great help for me.
I looked at adatapost's link. It seems like a world of trouble awaits you down this path.
I mean, I like Cygwin a lot, but I wouldn't use it like this.
A few centimetres to the right of the 'Your Answer' box I'm typing in is a link to a Related question 'How can I run cygwin from Java?'
Who's putting the cart before the horse? I don't know.
Does Cygwin have to be involved at all ?
If you are trying to run a binary that requires the cygwin1.dll (which includes most commands you can execute from the cygwin bash shell) then you can run it by specifying the cygwin\bin directory in the path environment variable like this:
Process p = Runtime.getRuntime().exec("C:/path/to/cygwin/binary.exe", new String[] { "PATH=C:\\cygwin\\bin" });
This assumes you installed cygwin in C:\cygwin

Categories