Hosting the Java compiler in an applet or in Google AppEngine? - java

Searching the web I've found that the Javac compiler is written in Java, and I also peeked at the source on Sun's site. The source is quite big and I couldn't make any headway on it. Also the Eclipse project has a compiler embedded inside, but who could touch its source code ;-).
So I thought I'd throw a couple of questions your way:
Could the Java compiler be hosted in an Applet?
Could the Java compiler be made to work on GAE, with dynamic loading of the resulting class files from the datastore?

Yes, the compiler as such is really just a normal Java application (except that it usually brings its own native launcher, but that's not required).
So you can easily run it within an Applet or inside GAE.
However that won't really help too much, because if you want to actually run the produced classes, then you'd need to play with ClassLoader instances which is not allowed in (unsigned) Applets and probably not allowed in GAE.

..Could java compiler be hosted in an applet?
Only if you add the tools.jar to the runtime class-path of the applet. See Add the compiler to the application's runtime classpath in the STBC help for details.
..Could java compiler be made to work on GAE, ..
Not sure, though note that someone seems to have registered a Google app. by the name of 'javacompiler'. ;)

Related

Multiplatform standalone Java application with JVM included

is there a way to execute a jar on different OS (linux/windows) that could be not installed jvm on?
I take a look on lunch4j but it make a search of jre on OS. This isn't my case.
Tnx
alot
1. Check following Stack Overflow answers (and its many duplicate links) Compiling a java program into an executable. You should find suitable tools that will allow you to create an easy to deploy form of your application, e.g. *.exe for Windows with all the necessary run-time libraries optionally included.
2. For inspiration of how such deployment might work (either run through the web browser click or easy to download/run installer) check yWorks yED. If you like their way you may send them an "share your know how" e-mail

Integrating Java compiler in my application

I want to integrate java compiler in my application but I don't have any idea how will I do that.
I want that user could compile and run code on my application.
If anyone can help me then please tell me from where I should start.
Starting Java 1.6, the standard JDK ships with an API to the compiler. Meaning, you can easily write a Java program that will compile other programs.
http://docs.oracle.com/javase/7/docs/api/javax/tools/JavaCompiler.html
EDIT as per Aaron's comment: an application using the compiler API must be run using the full JDK. The JRE doesn't include the compiler API implementation.
A second alternative is to embed the Eclipse Java compiler into your code. The advantage of this is that your application doesn't need a SDK to run - just add this single JAR.
Details are documented here: http://help.eclipse.org/kepler/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Ftasks%2Ftask-using_batch_compiler.htm
Note: Adding a compiler to an application creates an enormous security risk. It basically means anyone can start doing anything with your application. Things that spring to mind are:
Attacking other computers, people and sites
Looking at all the data on the computer on which the application is running
Analyzing the network the computer is in and attacking any other computer in the same network
Installing Trojans or viruses on the computer

Java LoadLibrary UnsatisfiedLinkError, though Java can see the file and runs on another computer

This problem is really befuddling me and hopefully someone could help me out. I've written some plugin libraries for a large java software. Everything works fine and well on my development computer. However, after I compiled and wrapped the software in Nullsoft Installer and install onto another computer, everything but the native dll plugins I developed runs, and I get a UnsatisfiedLinkError when I call System.loadLibrary. The installer works on my dev computer, the installed software runs fine even after I moved the original software. Here's what I do know:
the native libraries are deved in .NET (C++/CLI) hooked in by JNI
The software on the new computer is loading the right library path, can see the native dlls. In Java, I've added a segment to check the permissions on the files using File class, Java runs fine on library_dll.canRead() and library_dll.canWrite(), but hangs/crashes on library_dll.canExecute().
If anyone can help me, I would really appreciated it!!! Thank you all!
Does the target computer have .NET installed, and the right version of .NET at that?
I can't say I've ever done any .NET/JNI interop - it sounds potentially tricky to me. Have you looked at using JNBridge to make things easier?
Thanks Jon! No, the target computer does not have .NET installed (at least none more than the ones windows have at default). But it seems like JAVA/JNI can't even load the dll... I will try installing .NET framework and see
.NET/JNI interop was not the difficult part, that is working on my dev comp... It was tricky but not too difficult.
I would suggest that you run FileMon on the computer which is giving the problem, and then try to run your application. After FileMon captures data, you should see all file accesses made by your Java application and see the exact failure which leads to the UnsatisfiedLink error. For example, it may be that some dependency of your JNI DLL is missing on the other computer (this sounds like a good possibility, after security/permission errors).

Downloading JAR files at runtime in Java

I am wondering if it is possible to have a Java desktop application, on startup, look to some URL, to see if it needs an update, and if so download necessary JAR files, and add them to classpath for the running program.
If the old jars are there, they shouldn't have been loaded into the classloader yet, at this point should they? Is it possible to swap them out before they are loaded w/out restarting the application?
JNLP (Java Web Start) will do all of that for you.
It should be possible. The URLClassLoader is a place to start.
It's possible but prone to error.
A somewhat cleaner approach is to have a "launcher" application which does not rely on any of the updatable JARs do the above and then launch the actual app.
You may want to check out this question for more details on this topic.
Java Web Start is probably your best bet. The required jar files should be identified in the descriptor allowing your to manage the version control.
If you have a dynamic application that needs to discover and download classes dynamically at runtime, then you could look at Dynamic code downloading using RMI.
Java Web Start is a good choice but has some quirks. Notably:
Code which is updated must be downloaded before the program starts. A more modern approach would download updates and apply them at next start.
Default is inside a sandbox. If you need to go outside of that you must sign your code, which is rather cumbersome until you automate the deployment process (or use Netbeans which helps quite a bit).
Problems are not easy to debug - only tool is enabling full trace in the Java Console.
Caching of jars in the client is error prone. When you update, be sure that the URL is unique for each deployment component so the cache will not be used.
But WHEN it works it works pretty well. It is to my knowledge the easiest way to have a centralized version easily updateable of a given Java application.
--
EDIT: It appears that the "start program and transparently download updates if available" functionality is present in the latest Java 6. I have not tried it yet, but will soon.

What's the best way to add a self-update feature to a Java Swing application?

I'm trying to figure out a way to add a self-update feature to a Java/Swing application I'm working on.
Basically I've got a bunch of jar files with extra functionality to be re-deployed to the installed users when they change. Nothing complicated, just check if a new version has been released, download them over HTTP, and then optionally offer to restart the app to the user.
I had a look at webstart, and it could work. But this particular app does some funky stuff with classloading and GC memory settings that don't look like they are supported via webstart, or will at least complicate matters. (It's a tweaked build of JMeter)
I also went down the road of adding in this plugin handler http://swing-fx.blogspot.com/2008/06/add-auto-update-and-plugins-to-your.html, but it is very alpha, and tries to do too much with the usual bugs you get with alpha stuff.
I did the exact same thing. But that was long back so there are probably better tools today.
What I found out I needed was a loader. The loader main program did not have the app jars in classpath. It first downloaded an update if required and then created a custom classloader with the app jars in class path and invoked the main method of the application main class. It is not very complicated. IIRC I needed to do this because the jars could not be overwritten in windows if they were already in classpath.
Hope this helps.
we had a swing app 6 years ago that had self-update. like you suggested,
1)it downloaded the latest jars over http,
2) copied them to a folder.
3) since the swing app is launched using a .BAT file, after user said YES, we would shut down the swing app and look for any files in the update folder. if yes, launch another .BAT file to copy the NEW JARs to the required directory.
4) then re launch the swing app.
Updates, plugins, separation of concern etc. are exactly what OSGi is about - you might want to take a look at this. It won't come free (read: with a steep initial learning curve, especially when you are currently using classloading tricks) at least there are good open source implementations (felix - see felix.apache.org, equinox - see www.eclipse.org and others)
For these implementations autoupdaters are available - if you write your modules correctly it's possible to update at runtime without restarting.
I believe you should look again at Java WebStart, or at least detail the "funky classloading" which you think is going to cause problems (as it might also cause problems with any solution proposed here).
IIRC, you can set command line parameters using Java WebStart ( http://java.sun.com/j2se/1.5.0/docs/guide/javaws/developersguide/syntax.html#resources ).
I would definitely first try out Webstart. We've had lots of success launching even the early Eclipse RCP apps using Webstart, and you can probably not get more funky classloading issues than with the OSGI framework (Eclipse Equinox).
Could you perhaps give some more detail in your question about you classloading approach?
Regarding the GC and other VM settings: these are easy to specify in your JNLP (Java Network Launching Protocol) files used by Webstart for launching apps.
The Java Web Start is good choice. The GC stuff is not important. Classloading could be problem. But when you got trusted by user you can grant AllPermisions and you will be able to do custom classloading. Maybe it will be good to reconsider funky stuff with classloading. It is really necessary? Or look at NetBeans. There should be found inspiration for auto-update.

Categories

Resources