server side compile and running languages - java

In my website my users will submit source code of languages by using a html form . I want to
compile and run this code belonging to different programming languages
like c,c++,java etc... on the server and return the output or errors to the webpage.
many clients upto 3000 submit such codes every week and i need to run them and show output.
I must need java and c++ language to work ,but other languages support is appreciated.
Ineed to implement this on my site codersadda.com where you can find programs and i want to show output for them

It sounds like you need a little front-end for the compiler to make it comply with something like FastCGI. The exact interface will depend on the web server you're using, but the general principle remains pretty close to constant -- invoke the compiler, capture its output and route it back to the server to be formatted as HTML and sent to the user.
Edit: sorry, the first time I read this, I didn't notice the "and run" part of the question. For that, nearly the only reasonable choice is to run the code in a virtual machine. Basically, set up a virtual machine and create a "snapshot" in its pristine state. Then for each program run, you'll restore the virtual machine from the snapshot, and run the user's program. The tricky part will be spawning a program to run inside the VM, and capture its output when it's done. I'd guess that's possible, but I've never tried to do it, so I'm not sure exactly how -- and even if I did know how, it probably varies from one VM to another anyway.

Related

Call Unix Shell commands from Servlet/JSP

I need to call some Unix commands from my Servlet.
I have some Perl script, but I want to "translate" them into Java.
Here is something that I want to do on Java, but that I've made in Perl:
system("myfolder/myscript.sh > /myfolder/logs/myscript.log");
Is it possible to do this on a Servlet?
Yes, but note that redirect is part of the shell you will want:
ProcessBuilder pb =
new ProcessBuilder("/bin/sh", "-c", "myfolder/myscript.sh > /myfolder/logs/myscript.log");
pb.start();
Short answer:
it's possible but it's bad design, and can pose a security risk.
better to flag somehow that the script needs to run and check the flag via script
Long answer (following the commments):
Servlets are usually used to provide a user interface (or api) to something, for example accessing data or in your case triggering an action. As such, they imply the possibility of access from a remote resource such as a remote computer. In some (actually most) cases, that remote computer may even be out of the network, for example somebody's home.
Every server which is exposed to the outside world has the potential of being hacked or attacked in some way, with the risk being directly related to the level of interest this resource poses.
For example, if you work for a big company (which is then noticeable by hackers), and this servlet is used to trigger a build in your local repository, and you decide that developers will be able to work from home and need to login in order to trigger a build or check their build status, it means that anyone with the right credentials can potentially access the servlet, from anywhere in the world. Now lets assume that your perl script needs to access your CI server for some data, and your source repository for another data (maybe it even copies the sources instead of letting the CI server do it). In this case, you just created a direct link between someone sitting somewhere in the world, to the company's source code. It also means that even if it's too hard to penetrate your incredibly secure service because you spent a vast amount of time closing all potential gaps, they may still be able to trigger many unnecessary builds, and if you work in Continuous Deployment even make those builds go to production (maybe causing a DOS attack or service disruption). If at some point someone decides that the script also needs to get a parameter from the servlet, you've even made the hacker's life easier and could eventually give him access to your system.
All I described in the previous paragraph may be completely irrelevant to your case, you might be developing a service which will run on your home computer and won't interest anyone but yourself, but this does not change the fact that this is bad design (which might be ok for home use by the way).
What I said in the short answer is that it's better to have servlets flag the system that an action is needed, for example set a flag in DB or even in a file, in this case a hacker's life would be much more difficult, as there's no direct link. This also makes the servlet respond immediately, possibly automatically updating on status, instead of waiting for the perl script to finish running.
Did u try Jsch.It can do ssh and execute shell commands.

How to execute console or GUI input as if it was actual Java code?

I want to be able to input java commands/code to execute during run-time (during the execution of a "persistent" program) in the same way, or as close as possible, to the same code would be executed if it was present on the source-code for the program (programmed as part of the software), using a GUI element, like jTextArea.
The following StackOverflow questions seem to be related, but, along with they'r answers, don't seem to be what i'm looking for.
How To Get Input From Console Class In Java?
Using GUI for console input and outputin java
I don't want to "be able to trigger actions from specific console inputs, and print the feedback into the console", as seems to be the case of console applications described in those question and answers.
What i want is to "be able to execute actual java code, that i cannot predict nor control, from console (or GUI) input".
NOTE: The "i cannot predict nor control" here refers to the "developer"...But of course i can predict and "control" it if i'm the one inputting the code, as the "user" would do.
Since java uses a virtual-machine environment, i think it might be possible for it to execute "run-time inputted code"...But i'm not sure such thing is even possible.
For example, i'd like the run-time input of for(int i=0; i<3; i++){System.out.println(i);} in the "GUI console" (jTextArea, for example) to, upon pressing the enter key or clicking a "Send" button, be ("compiled and "?) executed, and give the same output as if it was part of the original source-code, thus:
0
1
2
So i'd like to know the following:
Is it possible?
If yes, how can i do it? (OR, if no, what is the closest alternative?)
Use the JavaCompiler. It can compile code from a String.
For an E.G. see the STBC & especially the source code. It provides a GUI and can compile the code in the text area on button click.
But note the:
System Requirements
STBC will run on any computer with a version 1.6+ Java Plug-In* JDK (AKA SDK).
(*) The API that STBC uses is merely a public interface to the compiler in the tools.jar that is distributed only with JDKs (though the 'public JRE' of the JDK also seems to acquire a tools.jar). This leads to some unusual requirements in running either the native jar, or the web start app.
You can use JavaCompiler, as this question's answer states:
https://stackoverflow.com/a/935316/420001
Also, what you're wanting to do is evaluate a String of code:
It's not really recommended though.
There is a project called BeanShell
"In short, BeanShell is dynamically interpreted Java, plus a scripting language and flexible environment all rolled into one clean package. "
I know this is an old answer but for future Googlers:
I would recommend JavaREPL whose source is available here:
https://github.com/albertlatacz/java-repl
What AlmightyR is asking for is called Read-Eval-Print-Loop (REPL) for the Java language which is what JavaREPL provides.
JavaREPL has an online demo available here: http://www.javarepl.com/console.html
JavaREPL also has an Intellij plugin and a CLI version which are both linked to in the Github repository.
It looks sort of abandoned currently but perhaps it just doesn't need to be maintained?

Using a Java program I made on a Web Server

I created a small application that, when run, creates or updates some tables in a database by extracting data from some PDF files. Everything works fine in this desktop application, but the next step for me would be to make it possible for an administrator on a website to upload a PDF file and my Java program would then run and update the tables accordingly.
The problem is I have no idea where to start with this (the site isn't done yet, but I'm running some tests and it is going to be coded in PHP). I'd like to know what kind of technologies I need to let the server run the program and update everything as it would in the offline version. Sometimes it takes a while to update everything, so ideally, the user uploading the PDF could continue browsing other pages while the server does its job. (I'll probably implement something that when the server is done processing the file, it says if the program ended successfully or not in a log file)
Can someone tell me what terms to search for on Google or give me some pointers? I haven't chosen where my website is going to be hosted either, so if someone could tell me what to look for to know if they support running applications like this, I'd really appreciate it as well!
This could also apply to other programming languages as I know a bit of Python and C++ as well, so in the future I might have some applications in those languages I'll want to use on the web.
If I'm not approaching this the right way, I'm open to other suggestions, but the best solution would be to keep my Java program intact as I know it works exactly like I want it to and I'd rather not have to start it all over again.
If your host is *NIX based you can use crontab (Automatic Task Scheduler) to run your program at set intervals. Make it check if a "new" PDF exists, and run the program if there is. There may be a way to use Windows Task Scheduler type programs to do it on Windows. This is probably the easiest way.
Alternately you can use You can use shell_exec() in your php to execute a command on your *NIX system directly to run your java program.

Run a given program by user

In this question's answers someone can find many sites (like ideone) allowing a user to write and run programs online. I'm interested in making something similar (only for Java code though) and was wondering on how can this be done.
A simple idea would be take user's given code, send it to server, compile it, run it and then send back the output to the user. But what if the user has malicious code like deleting my server files, etc.? If I wanted to create the same thing for the C programming language, I guess I could just get the assembly code of the C file, see what system calls are being used and decide whether the given code is malicious or not. Based on the previous idea, should I look in Java, the program's created bytecode? Is there any better/easier way to do it?
Don't reinvent the wheel, Java has the SecurityManager mechanism to restrict potentially malicious code
Reference
the Security Trail of the Java
Tutorial
Permissions in the Java™ SE 6
Development Kit (JDK)
In addition to what Sean Patrick Floyd said in his answer, you could also sandbox the process itself on your Operating System as an added measure. Create a new user on your host (the machine that executes the code) and assign it very few permissions (only what's required to run the code). Run the process as that user.
That way regardless of the SecurityManager present, the spawned process won't be able to harm anything.

Listening to print job event invoked using File menu from Java

I'm interested in knowing how can my Java program listen to the Print event generated by the underlying OS. As my project is based on Java, the Print job event listener should not be platform depended.
To be more specific my program will be running on the background and should be notified of the print job if a user is trying to print pages from MS Word (for an example). The event should notify not only the print job being started but also other details such as No. of pages, the document location etc (if that's possible). A link to complete tutorial or some snippets would be much appreciated. Thanks!
We are a C++ and Java shop. I have written code that does pretty much what you are asking for. It was written in C++ for Win32 (and was a right bugger to get correct - this is one of the least well documented areas of the Win32 API - and different printers send different event streams, so it can be really tough to develop robust print queue handling).
If someone has created a JNI library for doing this stuff (unlikely), there is no way it will be cross-platform. The effort involved would be enormous (different OSes handle print queues and notifications in completely different ways).
My recommendation would be to brush up on your Win32 programming (with C/C++). Once you have things working there, if you absolutely must have them interact with your Java app, you can use JNI to wrap it.
Here's MSDN articles on printer change notification monitoring (you'll have to call OpenPrinter first - but the next call is FindFirstPrinterChangeNotification): http://msdn.microsoft.com/en-us/library/dd162722(VS.85).aspx
If you are trying to support *nix and mac as well, you'll need to dig for those separately. Good luck.

Categories