How to load existing Java code and run it within the program - java

I’m creating a mock OS in Processing 3, and I want my OS to be able to download and run apps which have been written by a third party.
Basically, I want my OS to be able to load an application file from the user’s computer, get the Java code from the file and run that code within a window object I have created within the OS. Is there any way I can load Java code and run it within the program currently running?
I have hard coded a Settings, Calender and Notes app into my OS, but ideally they would also be kept in files of their own and loaded during the bootup sequence, as would any other applications the user has chosen to install on the OS.

Related

How to deploy a Java Play App in production using a Windows environment?

I have built a Java Play app using the latest version. Currently I am running the application using the batch file that gets generated. There's a zip file created inside target->universal folder (When I go to the bin folder inside the extracted folder this batch file is available).
How can I run this in production Windows environment? This play app is a backend service that needs to run continuously. Should this be created as a Windows Service? As in, should I try to run this batch file as a Windows Service through some means? I can't find any online references on this. Any help would be much appreciated.
There are service wrappers available, that can take almost any arbitrary application and run it as a service or daemon on some operating systems. I would highly recommend that you avoid them. Writing secure, performant services for Windows is not trivial. You can however run any script using Windows TaskManager (GUI) or Task Scheduler, and it has all the knobs you need to easily control how your application runs, when it starts and what to do if it crashes.
See schtasks.exe for command line interface reference.

Is it possible to automatically upload files when created to a web server?

I am working on a project using a Raspberry Pi and a web cam to detect motion.
I have got it to a stage whereby it takes an image and saves it on my computer. What I am wondering is, is it possible to let FileZilla automatically upload the image to my webserver when a new image is taken? Or is there any other ways that I could achieve this?
Since the post is tagged java, I'm assuming that you're using a Java program already or have the basic knowledge to create a Java program.
On to the answer: yes, you basically have two options.
1. Upload from within the Java program. FTP is probably the easiest since most web servers will have an FTP server running. Here is a tutorial you can use: http://www.codejava.net/java-se/networking/ftp/java-ftp-file-upload-tutorial-and-example
2. Use another utility outside your Java program to upload the file to your webserver. rsync would be the tool of my choice (tutorial here). When on a Linux machine (for example, the Pi) or a Mac, you can run a script that syncs the content of a local folder to a remote folder every x seconds:
while true; do <rsync command hier> sleep 5s; done
Note that that sleep period shouldn't be too short or you'll end up running multiple instances of rsync.
When on a Windows machine, you need to find another way to run a periodic process to trigger the rsync.

Message Passing from NetBeans Plugin to C# WinForms Application

The short version of this question is, how can I send a message (a file path, for example) from a Java application (NetBeans plugin, to be specific) to an already-running C# WinForms executable application?
Some details.
I currently have a Windows application that I built (it's like a parser/editor for script files to help automate the process of script building using simple GUI tools for our test team). In the application, you can load a file using the app's "Load" button, hotkey, drag/drop, etc. You can also open the application from the command line with one or more file paths as parameters, and the app will launch with the given files already loaded.
On request, I cobbled together a NetBeans plugin (Java) that grabs the file path of the file that is currently in focus in NetBeans, and then executes my application with the focused file's path as parameter. This allows me to launch my app directly from NetBeans and open the file being edited in NetBeans in my app.
So what my app can do:
Launch with file loaded from CLI parameter
Load file from internal load command on-the-fly
What it can't do (what I want to add):
Load file in running instance from external message parameter on-the-fly
Load file in running instance from CLI message parameter on-the-fly
MSMQ is a solution I can't use. Saw it suggested in a lot of other threads. Things to note are that the NetBeans plugin and my app will always be on the same system, but I can't get MSMQ on all target machines.
Two suggestions:
(1) If you can figure out a way to publish an event from a Java process into the windows event log, you can definitely set up your .net app to watch for specific event types
(2) If you can arrange for the .net app to watch for files being created in a particular directory using FileSystemWatcher (maybe in %TEMP%), you can have your Java process write a file that contains whatever info you wish to pass.

Write to C:\Program Files from Java program

I have written a Java application that includes a self updater. The self updater loads new program versions from a web server and replaces the application files. While this works perfectly if the application is installed e.g. in the users home directory, it fails on windows machines if it's installed in the C:\Program Files folder. This is because the JVM is executed under the user’s account which has no write access to the program directory.
If a native program, e.g. an installer, tries to write to the program folder, usually a popup appears asking the user to permit the write operation. This doesn’t happen for java applications. Why?
Is there any way to achieve that a write operation of a Java program to a restricted folder brings up the security popup so that the user can permit access to that folder?
Thanks for your responses. According to the answers I see the following options:
Java Web Start
For me this is not an option for end users. I think that no one can expect from an ordinary end user to know what Java Web Start is, what it’s good for and how it’s used e.g. I doubt that an ordinary Windows user knows how to uninstall a Java Web Start application.
Use an exe-launcher with manifest to launch the Java application
As far as I understand this solution the exe-launcher would request extended execution right at application start. This is not exactly what I want, cause for my use case it would be sufficient to get extended rights if an update is available and not on every application start.
Perform the update operation by calling a native executable
One could call a native executable to let it perform the update operation. In this way the application would only request extended rights if an update is available. This sounds not bad but includes some native coding for Windows and doesn’t work on other platforms.
Install a launcher in program folder and the application in user home
One can place a launcher in the program folder that calls the application that is installed in the user’s home directory. In this way it would be possible to update the application in the user’s home folder.
I use InnoSetup for installing my application on Windows and as far as I can see it a split installation is hard to achieve with this installer and probably with other too.
Install the complete application in the user’s home directory
Because the user has write access to his home directory there is no problem at all. For me this looks like the best option cause of its simplicity.
If you are using inno setup compiler to generate your launcher, then you can change your app directory permission.
For example, if you need full control and want to update files under AppName/data folder
[Dirs]
Name: "{app}";
Name: "{app}\data"; Permissions: everyone-full
[Files]
Source: data\*; DestDir: {app}\data\; Flags: ignoreversion recursesubdirs createallsubdirs; Permissions: everyone-full
Unfortunately the increased permissions need to be requested when you first start the program, you cannot promote to them later. Programs that look like they do that are actually restarting themselves with the higher privs behind the scenes.
I had a problem like this a few years ago with a Java app and in the end I installed the application to the user data folder instead of program files as otherwise the auto-updating was a nightmare. You can still add it to the start menu so to a user it looks exactly like any other program.

'Secret sauce' in MacOS X private APIs for Java - request for further detail

On his blog Scott Kovatch writes:
Without getting into too much detail, typing ‘java MyAWTCode’ from a Terminal window violates a whole lot of assumptions about what an application is on Mac OS X, and needs a lot of cooperation between the AWT and the Process Manager to sort it out.
http://skovatch.wordpress.com/2011/01/03/secret-smoke-screens/
Out of curiosity - what assumptions are violated? Surely this is just a candidate for an API call with callbacks?
Sure, I can elaborate on that a bit.
The Process Manager starts with the assumption that all applications that present a UI on Mac OS X are bundled, are packaged in a folder named Application.app, binary in Contents/MacOS/Application, and most importantly, have an Info.plist to get things like the name of the application that will be shown in the application menu. When you run a Java application from the command line (Swing or SWT) there is no Info.plist, so we had to create a CFDictionary to be passed off to a private SPI that would register the application, give it a proper name in the Dock -- as opposed to just 'java' -- and could be force-quit.
Even then, it's not perfect, because the Dock also assumes it can store an alias to the bundled application when you right-click and choose Keep In Dock, but since there isn't one that fails silently. There's no way to store a shortcut or command line to start the application like Windows can.
The SWT just calls TransformProcessType, which is a start but is nowhere near sufficient to turn a Java application into a full UI application. For doing pure SWT testing and development it's enough to get you going. When you create an Eclipse RCP-based application for deployment you end up with a bundled application with the Eclipse launcher, and plugins and features, and you're ready to go.
Of course, if you go the extra mile and package your Java application into a bundle, this is all moot, but the vast majority of developers coming from other platforms don't bother and just want to run an executable JAR file or even a folder of class files with a shell script.
I am not sure what he had in mind, but I guess a big difference is the file structure: a normal MacOS X application is a bundle with the structure NameOfTheApp.app/Contents/MacOS/NameOfTheApp , and specific files in the Contents directory. When we use the terminal with a "java" command, the JVM has to create a "virtual" application specific to the Java code, and handle all the MacOS events for it. Also, when you open an application twice in the Finder, it simply activates the application the second time, while you need to launch separate applications every time you use "java MyAWTCode".

Categories