I am trying to work on a desktop application using JavaFX/Java that requires me to compile and run c++ and Python code when it's requested, offline. I was wondering if this even possible?
How can this be done? Are there any libraries that can compile other languages' code and run them, which can be added in JavaFX/Java environment? Or do I need the user to install c++ compiler and Python in their machinees and then somehow integrate those tools in my application?
I would appreciate any help or insight.
Packaging a C++ or python compiler into a Java application is impractical. Your system would have to store, unpack and install each compiler into the local directory, resulting in a massive file size, it would also take a very long time.
The best method is to require that the user have those compilers installed or provide them with the install utilities so they can install them separately.
Once the compilers are installed you can get Java to run a shell script which builds and compiles the files.
I hope that solved your question, if you need any further clarification please feel free to reach out to me.
Sincerely,
Owen
Related
I'm working on a program that utilizes Caprica's VLCJ Bindings.
This is fine and good for Windows and Mac, as I can just package the VLC Libraries for them in a zip file and output them to the user machine where appropriate.
The problem comes when I need to do this for Linux, because, dear god,
there are ELEVEN. SEPARATELY COMPILED. APPLICATIONS/LIBRARIES.
And some of them even have their own flavors. It's like the Baskin Robins of OS's (I knew there were a few but I've only really ever run Ubuntu so I was not prepared for this).
If I was a masochist I could totally make this work and end up with a gigantic jar with an absurdly huge number of zipped Linux libraries within it, but I really, reHEHEHEALLY do not want to do that.
So I figured that the best course of action to take would be to check if LibVLC is installed, and if it is, reference it directly, and if it isn't, install it at run time (before the library tries to load itself), or, heck, even at launch/install time for the Java program.
Is this possible? I know that on Ubuntu using the terminal it would be something similar to
sudo apt-get install vlc
and there's probably 15 different flavors of that, which is fine, I can deal with that, but is it possible to do that from within a running Java application (and wait until it's finished before moving on), and if so, how can I go about doing that, and if not, how hosed am I?
To detect if LibVLC is already installed it's possible to do that in pure Java simply by searching the file-system using Java File IO. The idea is you'd look for "libvlc.so.*" in "/usr/lib" and/or other "well-known" locations.
vlcj provides a NativeDiscovery class that will do this for you:
boolean foundLibVLC = new NativeDiscovery().discover();
You can find Javadoc here.
You could then throw up a dialog box to prompt the user to install vlc using their package manager.
Or you could detect the OS by using Java File IO to read "/etc/issue" and if it were Ubuntu you could launch a process to run "apt-get install", or a different package manager for a different distro. I've used Apache Commons-Exec to do this sort of thing before.
There's simply no universal package manager you can assume across the different distros.
I know it's not ideal, but this is what I do for my projects.
To be fair, if you're targetting users on Linux you're likely to be targetting more technically savvy users than most and they would likely have no problems installing other software.
A further option I suppose is to ship the VLC source code with your project and have your installer build VLC. It takes a while to build VLC from scratch though, and there are lot of third party library dependencies that must be installed first.
I suppose on Linux, the 'correct' thing to do is to create a distro package (like a .deb or .rpm or something), declare a dependency on VLC, Java and JNA and do it that way.
I've looked at several questions and read through a couple of tutorials but MakeFile is still a bit of a confusing concept to me.
From what I understand, it is essentially a set of rules for building up Unix commands to compile and run the code?
So far, I have been just running my Unix commands as such:
>> javac Main.java SomeClass1.java SomeClass2.java
>> java Main input_file.txt
because my Main function takes in an input_file.
I want to be able to make this more efficient by using Make, but I am having trouble with understanding the concepts.
Any help is much appreciated!
Thank you!
Make is a build tool - a piece of software to compile the source code of software projects into an executable.
When you are creating small, simple programs, you don't really need a build tool. You can just compile your code by running the compiler javac on the command line. But when you start working on a larger project with many source files, it's going to be too cumbersome to compile all the source files by hand. You'll want to use a build tool. Besides compiling your code, a build tool can help you perform other tasks, such as automatically running unit tests and automatically managing dependencies (libraries that your program needs).
For a Java project, consider using a Java build tool such as Apache Ant, Apache Maven or Gradle. Those are the de-facto standard build tools for Java projects, and the big Java IDEs (Eclipse, IntelliJ and NetBeans) have support for these tools.
Make is mainly used for C and C++ projects and is not very well suited for Java.
New to java programs so apologies for the noob question. I have written a java program that I run using "java myprog" command in a terminal window. I want to give this program to friend but not sure what that person needs as a minimum to run the program. Can the code be compiled to run completely independently of any java installation my friend may or may not have? Will the other person need to recompile the code?
Option 1
You can give them the compiled output (the .class) file.
They need a version of the JRE (but not the JDK that developers need to compile) that is compatible with the version you used to compile your code. The simplest way is just to match versions. For example, if you're using J2SE 7, then get JRE 7 for your friend.
With this your friend should be able to type java myprog and get the same results as you. If you want your friend to run the program from the command line the same way you do, then this is probably the simplest approach.
So, if on both your machine and your friend's machine you type:
java -version
...and you get the same major version, then you're probably in the right ballpark.
Option 2
You can also create an executable .jar file if you want something your friend can just double-click, but they'll still need a version of the JRE installed, and creating executable JARs can be a bit difficult if you're new to the language. They take some learning and trial-and-error time. This is definitely a more complex approach than option #1.
Option 3
There are compilers that will convert your java program into native code (an .exe file in Windows), but you'll need to know what OS your friend is running, and make sure you're compiling for your friend's OS.
One example of these tools that I heard about years ago is Excelsior JET (though I'm not endorsing it as a tool - just saying that they exist).
This is definitely the most advanced/most difficult of options, and includes even more trial-and-error than option #2, but if you're talking about a really simple program with a single file, then it might be very straightforward.
Of course, Excelsior JET wasn't free last time I checked.
I've written an entire article on this very subject:
Convert Java to EXE - Why, When, When Not, and How
You want to create an executable .jar file. Provided that your friend has Java installed, he can run the program by simply double clicking on the file.
Java programs are usually compiled into Java bytecode - not to a native executable. Once compiled, the resulting class files will be executable on any JVM that is recent enough, regardless of the underlying hardware platform or operating system. In order to have a JVM, however, your friend would typically need to have an appropriate Java Runtime Environment (JRE) installed.
Alternatively, there are ways to distribute a program along with a JRE, but I believe that this approach is not trivial. There are programs, such as launch4j, that can automate the process somewhat, if you want to go that way.
The options given above suggesting giving the .class file and using java myprog may not work if the friend is not located in the directory where myprog.class has been placed, as the class path may need to be specified. So the friend may have to use java -cp path_to_myprog myprog
I am using Ubuntu and I would like to know the basic tools to install so I can begin develop in java.
With 'minimal' I mean the most transparent way without fancy tools and stacks etc. Like for minimal C programming you just write code and run 'gcc file.c -o myapp'.
Thank you
Just download JDK.
Minimal tools are javac for compiling and java for executing JVM. Both are in bin directory.
You need to install JDK to start with hello world
Eclipse IDE. It's probably the most comprehensive Java IDE. I understand that isn't what you're looking for but light, simple ones like JCreator are not free to use. I don't see the sense in developing without an IDE of any kind.
Install the JDK. An IDE is a minimal requirement for development in Java. Not all developers would agree, but IMHO its the most productive way to develop in Java. I suggest IntelliJ CE which is free.
This question already has answers here:
How can I convert my Java program to an .exe file?
(16 answers)
Closed 3 years ago.
I'm wondering how to package a Java application into a native binary for Windows, Linux and Mac OS X.
I know Minecraft does this, but I can't figure out how. This is what'd I'd like to do:
From NetBeans (preferably) or Eclipse, build the three binaries automatically.
Include native libraries for OpenGL et. all.
Obfuscate my code if possible.
If there's some way to mimic the Minecraft auto-updater feature, that'd be totally awesome.
So, are there any tools available to do this for you, or do I need to write a large bulk of XML to accomplish this?
To make a native binary for Windows, you would use a tool like Launch4J. On OSX you could use JarBundler. Minecraft simply distributes the jar file for Linux. I'm not aware of a native binary packager for Linux.
You could also compile your Java code via GCJ but that's probably not what you want, as there are limitations and compatibility concerns there. The native bundlers like Launch4j and JarBundler simply wrap your jar file and use a real JRE to execute it.
As for integrating with NetBeans or Eclipse, you'll probably have to write your own ant build file, especially since the solution varies from one platform to the next.
If you are using Java 9, you can also use Java 9 Modularization & jlink to ship a zero-dependency native app.
There is also maven-jlink-plugin that could help here.
Take a look at GCJBuilder plugin for eclipse. Not sure if it supports cross compilation as the command GCJ compiler does.
If the app. has a GUI and can be distributed from a web site, look into Java Web Start. JWS is supplied by Oracle, and provides auto-update amongst many other features.
Note that JWS uses Jar files, so no conversion is necessary.
I've used JSMooth for this in the past: http://jsmooth.sourceforge.net/
As mentioned before, this wrapper just looks for a real JRE to run it - it does not come with a bundled JRE.