By using the JRE or some other runtime environment, is it possible to compile Java code on a web server such as Apache or Nginx?
Specifically, I would like to have a web application that would accept user input (Java code), and when the user hits run, it would compile the Java into Java Byte code and display the result, like Eclipse does.
Does this sort of technology even exist?
Does this defy the logic of compiled languages?
Thank you for your time in advance.
Amicably,
James
The server running Apache would also need a JDK. You could easily write the input to a file, compile it, run it, and display the results to the user. This could be done with a CGI script, or a Java EE app or similar, running in Tomcat. A JDK will be required, a JRE will not suffice.
Consider the potential security risk with this, though. Depending on what the program actually does, it could result in problems on your machine or others.
Of course this is possible. But you need a JDK, and some server side scripts (PHP, Ruby, Python, Java ...) which takes the source code as input and calls the Java compiler to compile the code.
What you have to consider are the security implications. Every user could then upload code, which would be executed on the server. So you have to make sure, that the code runs in a sandbox.
There are several websites that do this. One that I've used is at http://www.ideone.com.
You can reproduce this with the JDK and various scripts or you can write your own compiler from scratch. Either way, you need a compiler of some kind to actually compile the code.
Related
I'm thinking of purchesing hosting from a company that has Tomcat/JSP install on their servers (arvixe.com/linux_web_hosting). I wish to know if I need tomcat/jsp in order to compile a .java file on the webserver. What I would like to do is for a visitor to upload a .java file and for the server to compile it into a java webapplet. For example:
Visitor uploads their .java file.
Server compiles the .java into a .class.
Server signs the .class and makes a .jar.
Once done the visitor can download the .jar and use it for personal use.
What would I need in order to do this? I already know how to do it on the computer with Java installed, but I would like to do it on the webhost as well. If I can do this I would like to do it with PHP if possible.
You can compile java directly inside of Java, if you are on 1.6 or higher... here's an article about it:
http://www.javabeat.net/articles/73-the-java-60-compiler-api-1.html
In principle, if the version of Java used by the Tomcat setup is Java 1.6 or later, then you can use the Compiler API to compile Java programmatically. If the server is running Tomcat, then I would really just do it in a Servlet -- I can't quite see the rationale for wanting to shoehorn PHP into the equation if you have a working Tomcat server and you want to compile Java...
However, it sounds like your application is "server functionality heavy" enough that you might want to think about choosing a hosting plan that isn't too restrictive: ideally I'd go for a virtual server with full shell access and allowing you to install whatever you need. If the company is telling you that you can only use a particular version of Tomcat/Java, then that would ring alarm bells for me. (Of course, if they're just saying "install what you like, but if you use this particular version, we will give you additional assistance", then that's another story.)
I wan't to create an applet that includes the Java compiler and is able to download and compile .java files from the web (or from a textfield for that matter).
From what I understand, the Java compiler is based on Java, so this should be possible.
However, one thing I wonder is wether the Java compiler is able to store the class file in memory or that it needs explicit disk access.
Also, I want to catch the errors javac produces, but this is natural.
Is there an article on this somewhere maybe? I couldn't find much on the case. What troubles could I walk into?
EDIT: I'm planning on using the applet with OpenCL, so I would have to sign the applet.
EDIT: Similar question here: Hosting the Java compiler in an applet or in Google AppEngine?
Java compiler is always with you. You should use java.lang.Compiler class. But I believe you will have issues with SecurityManager.
If I were you I'd prefer to compile code on server side. It means that I'd develop servlet that is able to get the java source and compile it. The resulted java class should be available over HTTP under path that is configured to be accessible by applet, so your applet will be able to run this class after compilation.
From the documentation on the SSCCE Text Based Compiler:
javax.tools.ToolProvider. The docs for getSystemJavaCompiler() mention that it returns "the compiler provided with this platform or null if no compiler is provided", but do not make clear why it might be null, nor the fact that the compiler will not be available to applets or webstart apps.
I want to create a web based java tutorials kind of project which involves jquery, css, jsp, springs etc .,
so I actually want to practice these technologies resulting a good product. The content of this tutorial can be edited by any user and rated by clients according to the chapter etc.,
I have few such ideas.
One primary idea being have a web based compiler and executor . Similar to w3schools. I know its difficult for a compiler of server side technology on a browser.
My questions are:
Are there any such open source java
compiler/executor services available
which can be used?
Is anyone interested to work on such
project (in case there isnt any)?
EDIT:
I am planning to use Compiler in java and Beanshell. Any better solutions ?
Google App Engine is the one, which one can you help to set up Free online Java Compiler, But for that you need to have an account to proceed.
Few other places you can defnitely Check
http://compilr.com/
If you have Applets,
http://www.innovation.ch/java/java_compile.html
EDIT : As Stephen Suggests, Download JRE and JDE and use it from oracle website
http://www.oracle.com/technetwork/java/javase/downloads/index.html
With http://www.browxy.com you can compile and run java console application and applets
You would need a script on your server that accepts the sourcecode, compiles it and runs it, however I'm pretty sure, you don't want to run arbitrary code.
If you still want to do it, take a look at Google's AppEngine, they do allow users to upload their (compiled) java programs and don't seem to have had any problems with it.
This question is interesting. In fact, Tomcat JSP server is an online compiler for Java Servlet. May help you a little in your case. (Not for traditional Java, though).
Imagine that you gave user an input form for Java codes ( preferably a WYSIWYG editor ), the codes will be written in a temp JSP file, and then your code compile and run the JSP.
But be careful , JSP is too powerful ( as it can touch file system, etc. ). Make sure your security measure is set.
I've used ideone.com before when discussing Java code with someone in a remote location. It is a web based IDE that handles Java as well as a lot of other languages, and is very useful.
If you are trying to do it yourself, you should be aware java6 and above have compilation built into the language in the javax.tools package.
You may want to check out Fixoncloud compiler. It's a web-based collaborative online compiler. Anyone can make changes to the code and you can also track history of your code. The compiler supports C, C++ and Java.
Depending on the permissions set to the code, the owner can also make his code completely private and permit only specific users to view/modify the code.
Here's the link to the compiler:
http://www.fixoncloud.com/Home/compiler/index.php?id=254
I'm wondering if there is a standard method for deploying applications originally written in Java, to iOS4 devices.
I assume that the application in original format cannot be deployed - is there perhaps an emulation layer that I can use, or a stable compiler that compiles Java to ObjectiveC?
Option 1:
Use one of several cross compilers, compiling Java to ObjectiveC:
http://www.xmlvm.org/overview/
http://www.flexycore.com/ispectrum-overview.html
Option 2:
Package custom JVM with java application, with restrictions that meet the latest agreement (including no byte-code download capability and no JIT compilation). No JVM specifically designed for the iPhone is currently (Oct 2010) available, though the IKVM might run on top of Monotouch, and Oracle may build a version of the Java SE for the iPhone eventually.
Option 3:
Cross compile Java to one of the existing interpreters that are already accepted on the iPhone (eg, cross compile Java to C# and run the app on monotouch)
With the new current iOS SDK agreement and App store rules, it may be possible for you to embed a Java applet with your own JVM interpreter and runtime engine (but no byte-code download capability and no JIT compilation allowed).
Another seldom mentioned possibility for deploying any non media or graphic intensive networked app, such as many typical Java applets, is to run a customized RDP or VNC viewer on the iPhone and and view a Java app that is being hosted and run remotely.
Mechanically translating some of your code will likely work pending finding a cross compiler / translator. Trying to run a Java based GUI on iPhone is just plain stupid in my opinion. So the smart thing would be to port the GUI by hand.
The problem of course is if the app is mostly GUI you might as well write the whole thing over. Likewise if the app uses APIs that there is not a simple translation for you again might as well rewrite the entire app.
In a nut shell I think Steve J. Was right here, the route you are comtemplating just leads to poor user experiences. It actually makes me wonder why you would even think that a Java based app would be successful on iPhone.
Can a scala program be running on a browser with disabled java plugin (as scala is compiled to a jvm bytecode)? In other words: is the jvm disabled provided the java browser plugin is disabled?
Or does Scala run server-side and I am confused?
If I estimate it correctly, you can have both: Server-side and Client-side.
In case you want to use it on the server, it's called a Servlet and compiled to a JSP-application.
In case you want to use it on the client, it's an Applet and runs in the JVM. And that's what the user disables in his browser-settings.
As far as I know it, java and scala compiles to the same type of bytecode which is executed by the JVM.
Therefore, you cannot use neither Scala nor Java if the user has the plugin disabled. The browser cannot distinguish between these two.
The only java program which "runs in a browser" is an applet; any class extending java.applet.Applet will serve this purpose. As a scala class can extend a java one, it is therefore perfectly possible to write an applet in scala.
However, this cannot run if java is disabled in the browser as scala runs on the Java Virtual Machine. (In fact, there's no difference between a compiled scala program and a compiled Java program - they are both bytecode, which is run on a JVM)
Applets are not particularly common however, and most scala code will be server-side in practice. When you see people talking about scala and the web, they are most probably talking about Lift, which is a (server-side) web-framework for Scala.
Scala is almost always run on the server or as a normal desktop "java" application.
I suppose it would work perfectly well as an applet, if it doesn't add any confusion to the security model, but if applets are disabled I presume you can't run any JVM code at all.
Regarding Lift, the server side runs Scala, but the client side runs automatically generated Javascript, which does not need the jvm.