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
Related
I am trying to use Emscripten's emconfigure and emmake to compile OpenJDK for the web. The end goal would be to be able to run uncompiled java code natively in the browser. However, running emconfigure ./configure throws the error
configure: The C compiler (located as /usr/lib/emscripten/emcc) does not seem to be the required gcc compiler.
configure: The result from running with --version was: ""
configure: error: A gcc compiler is required. Try setting --with-tools-dir.
I have been searching all day and I haven't found anything along the lines of this error. Is there any easy workaround for this? Should I even be trying to build OpenJDK this way?
Is there any easy workaround for this?
I doubt it. If you look at (for example) jdk11u/make/autoconf/toolchain.m4 you will see the toolchains that are supported on various platforms. Emscripten is not listed.
I expect that emconfigure is trying to use the JDK's autoconf-based configuration framework. And that framework is checking the compiler you are trying to use ... and saying Nope!
You may be able to modify the auto-conf macros to support the Emscripten tool chain. But the next problem will be getting the code to actually compile. And then to getting it to run.
Should I even be trying to build OpenJDK this way?
It is not up to us to say what you should do.
However, I would make some observations.
If you are expecting this to work without significant effort, you are sadly mistaken.
What you are doing is equivalent to trying to port Java to a new OP platform and hardware platform. This is is a massive project. In addition to just getting the JVM to compile, you also have the problem of getting the Hotspot JIT compiler to generate WebAssembly code.
This is probably a bad idea anyway. Once upon a time, mainstream web browser vendors did support Java in the browser. But there were so many "security issues" that (virtually) all vendors have dropped support. Oracle saw the writing on the wall, and in Java 9 they started the process of removing support from the OpenJDK codebase. Applet support and JNLP are gone, and they are planning to get rid of the SecurityManager framework.
What you would be doing is bring back Java support in the web browser ... with the security safeguards stripped out.
Basically, the Java SE platform is to rich to implement securely in a web browser.
If you are still keen on building a JVM using Emscripten, take a look at https://github.com/emscripten-core/emscripten/issues/3342.
But note that they are talking about building JamVM, which is Java 1.6, and hasn't been touched since 2014.
I made a java program that I want to upload on a website. I want it to be available to download for everybody. I read many things about Oracle OpenJDK and OcacleJDK but I do not really know how this is connected to my program. And if I made my program with the Oracle JDK, how can I change it to work with the Oracle Open JDK?
Edit:
Sorry, I do not really know that much about JDK's and so on... That I understand this correct, the JDK is not bound to the program. The end user needs to have the JDK installed on the computer. Because I am not sure, if I need to buy a java license.
And if I made my program with the Oracle JDK, how can I change it to work with the Oracle Open JDK?
No need to change it.
Understand that the Java platform is defined by a set of specifications. Any implementation that complies with those specs can run any Java-based app. To ensure compatibility, an implementation may be subjected to test suites published by Oracle (TCK) or by Adoptium (AQA).
If you write a Java app using an implementation of Java 11, for example, then that app can run on any other implementation of Java 11. And most likely, that app will run an any implementation of future versions of Java.
Oracle JDK is a product from Oracle that implements the Java specifications. This product is commercial, requiring a fee for use in production, with support available. Oracle also releases an unsupported free-of-cost product that is an implementation of Java that oddly does not exactly have a name, found here.
Both of those products are built from the open-source project OpenJDK. Several other vendors also release implementations of Java based on this source code. You can build your app using any of these, and run your app using any of these.
Your use of the phrase “Oracle OpenJDK” is not accurate. While Oracle founded the project, and provided the bulk of the source code, the project is also backed by IBM, Apple, and others contributing important parts of the code base.
Here is a flowchart to help you in selecting a vendor. The products found inside the blue barrel are built entirely or nearly entirely from the source code provided by the OpenJDK project. The two products outside the barrel use parts of OpenJDK, but have significant portions built from other source code to provide their special features.
I want it to be available to download for everybody.
Your app needs an implementation of Java to run.
Your user can have an implementation of Java installed locally on their computer. Then you probably should build a JAR file containing your app for distribution.
You can include an implementation of Java bundled inside of your app. See the jlink tool. You can then make a complete self-contained "double-clickable" app that runs like any other app. Search Stack Overflow to learn more.
Because I am not sure, if I need to buy a java license.
Some of the Java implementations require a fee in some circumstances, and that also buys you support in all the cases I know of.
Some of the Java implementations are free-of-cost. Of course you do not get the benefits of paid support, unless you engage a support service company separately.
The key concept to remember is that the OpenJDK project provides only source-code but not binaries/installers. For binaries/installers, choose a vendor following the flowchart I posted above. For newbies wanting a free-of-cost implementation of Java, I suggest Adoptium (formerly known as AdoptOpenJDK), as this is a cooperative effort involving nearly all of the other vendors.
That I understand this correct, the JDK is not bound to the program.
True, your Java-based app is never bound to a JDK (Java implementation). After deployment, the Java implementation could be replaced by another while leaving your Java app untouched, no need to recompile. A JVM launches first, and then uses a Java class loader to load your app’s classes from your JAR file. A JVM is actually the app running on top of the host OS. On top of the JVM is your app running. So: host OS > JVM > your app classes.
This means your Java classes are not bound to the Java implementation, not in the sense of a linker in conventional software.
Caveat: What I discussed above is generally the case. GraalVM is very special in that it enables ahead-of-time compilation, for a different runtime situation. But that is cutting-edge technology, and not commonly used. Project Leyden is a possible similar effort.
I suggest reading the white paper Java Is Still Free.
All of the solutions I found on stackoverflow suggest wrappers to register java application as windows service. My requirement is totally different. Please don't suggest wrappers for the purpose. The question is very simple I have java executable and I want to register it as windows service.
Phyiscal Path
Service Properties
Unfortunately we don't have backup of previous setup that installed it as windows service at the first place. Do I need some setup program or anything like that.
Not necessarily.
It is difficult to advise you on precisely what you need to do without more information on what you actually still have; e.g. an application installer, application JAR files, wrapper scripts, etc. Alternatively, if you told us what the application was, then maybe we could give you some hints on where to get installers, etc.
However, I can tell you definitely that registering java.exe or javaw.exe directly as a Windows Service will not work. These are not the executables for your Java application. Rather they executables for as Java Virtual Machine that will run your (real) Java application.
It is so easy task in case of Visual Studio. I want same support in Eclipse or anything else.
Well Java doesn't work like that. Java compiles to platform independent bytecode files, not to platform-specific native code. Sure, there are third party tools to generate exe's. However, using them is neither necessary, or desirable:
Why is creating an .exe from a java program not recommended?
(And asking for recommendations on what tools to use to do this is off-topic.)
Finally, if you take an arbitrary Java program and turn it into an ".exe" file, it won't necessarily be immediately registerable as a Windows service. This Q&A talks about turning an ".exe" into a Windows Service.
Create Windows service from executable
However, I can't tell you if the advice given there is appropriate for an ".exe" file created from an arbitrary Java app by some unspecified 3rd-part tool.
My recommendation:
If you are starting from scratch, use a Java Service Launcher / Wrapper.
If not, talk with whoever supplied and/or installed this application in the first place.
If you can't find any information about the application and where it came from, or if the vendor has gone out of business ... you need to urgently look for an alternative.
I have studied Java Web Start and found it complex and clumsy for my purposes. In addition, my app needs to access the PC resources, which causes more hoops to be jumped through with Java Web Start. To add to the difficulties I need to access a 32-bit native library (libvlc) so I need to insure that my app runs under 32-bit Java. Is there an easy way to deploy my app without resorting to Java Web Start? Needless to say, I want everything to be contained in a single .exe file.
I would start by searching the Internet for keywords such as "java 2 exe" and "jar to exe", etc. Doing so yields many freely available software packages that convert Java programs into Windows executables, for example:
JexePack - http://www.duckware.com/jexepack/index.html
JarToExe - http://www.regexlab.com/en/jar2exe/
JSmooth - http://jsmooth.sourceforge.net/
And the list goes on. Perhaps one of them meets your needs?
I am answering my own question to help people understand how to do this, which has taken me some number of days to figure out.
My initial problem was that my app would run on some versions of Windows but on others it was having problems finding the libvlc native library. I finally figured out that my app must run with 32-bit Java in order to use the 32-bit native library. Thus, the problem then became how to insure that the user started my app with 32-bit Java.
I read about Jar2Exe (http://www.regexlab.com/en/jar2exe/) in another post and decided to check it out. It is a great little program that is very configurable so I figured that it must be able to handle my 32-bit Java problem. In fact, it does so without even needing to do any configuration. The resulting .exe file contains my app along with all the required jar files and starts up with the 32-bit Java. I am very pleased with this program and plan to buy a license, which is very cheap.
Hope this saves time for other people who are trying to solve a similar problem.
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'. ;)