Using java applet in c++ program - java

I am doing a project in c++ where I need to embed a java applet/java program.( like we usually see on webpages). I was wondering what is the easiest way to do this. Right now, I am using Qt designer. Thanks.

If you use QWebPage in your application you can load a url that has the JApplet embedded.
Edit:
You may not have JRE installed on the target machine if it's not loading the applet. If you do something like below it should spit out a message if something goes wrong with Java. This is just an untested example to give you the basic idea of what I meant.
An example webpage:
<html>
<head>
<title>Java Applet</title>
</head>
<body>
<applet code="YourApplet.class" width=400 height=400>Java not supported or not installed</applet>
</body>
</html>
Save this as MyPageName.html
A simplistic example of using this in Qt:
// the QWebView has a QWebFrame and QWebPage to make it easy
QWebView* webView = new QWebView(parentWidget); // MainWindow or whatever as parent
webView->load(QUrl("MyPageName.html")); // local page or valid URL
webView->show();
Hope that helps a bit more

In version 4.8 of Qt, java applets are not supported.

Related

I want to run java applet in HTML, but it allways have bug

I try from a long time ago to launch Java applets in browser (they are simple games). For a long time it say out-of-date Java version (now have the latest version 8 update 45), even after I install the latest before 2 min. NVM, I hope I fixed it. Now it say a lot of different errors: runtime exception or AccessControlExeption for ex.
I am running this programs in Eclipse and I tried them, so they work. Maybe I have a problem with HTML code.
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<canvas id="canvas-id" ></canvas>
</body>
<applet width="600" height="170" code="theLightRoute.class"></script>
</applet>
</html>
First: your HTML code is not valid, remove the </script> closing tag inside your <applet/> section. Even if some browsers can accept it, I don't think you can expect to have a valid result with an invalid tag definition.
Second: the <applet/> tag support can vary regarding the browser (see http://www.w3schools.com/tags/tag_applet.asp). You should try with <object/> instead (see http://www.w3schools.com/tags/tag_object.asp)
Third: you cannot expect anymore that any modern web browser will support a java applet, whatever the tag you use. Indeed, most of them have dropped or disabled by default the browser-side support of any java applet for security reasons. For example for Chrome 42, the java support is totally dropped: http://www.infoq.com/news/2015/04/chrome-42-npapi. For Firefox or safari it should be enabled manually https://support.mozilla.org/en-US/kb/how-allow-java-trusted-sites).
Actually, there is no good reason to create some java applet anymore, it's a totally deprecated and obsolete technology (https://softwareengineering.stackexchange.com/questions/154537/do-java-applets-have-any-place-on-the-web-today).
Try to learn HTML5 for game programming instead :-) Many courses exist and can help you, and many HTML5 game engine are available (http://html5gameengine.com/). And it is cross-platform, mobile included!

How can I share my Java Applet?

Have just finished programming my first Java Applet. How can I share this with my friends without them using eclipse on my computer?
I have 12 classes in eclipse. I have seen some examples of people using HTML to embed their applet in a website, but
A) I have only found examples with only one class.
B) I tried following this method: http://www.oxfordmathcenter.com/drupal7/node/37
but when I click on the html file, it just opens the html code in my browser, not the applet.
This is the HTML file that I made
<html>
<head>
<title>
World Cup Game
</title>
</head>
<body>
<h2>World Cup Game</h2>
<applet
codebase=“https://www.dropbox.com/s/lcojvh8tm2mukzn”
archive = “WorldCup.jar”
width = 800 height = 600>
</applet>
</body>
</html>
I don't mind whether I share it embedded on a web page or if I send them an executable jar file or whatever, but does anyone know how I can share my hard work!? Thanks :)
(Apparently executable jar file isn't an option with applets though...)
Of course it is an option:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
<title>My Applet Page</html>
</head>
<body>
<applet code="ClassFile.class" archive="YourJar.jar" width="400px" height="400px">
Java is not installed on your machine or your browser does not allowed Java Applet to run<br /><br />Get the latest Java technology at http://www.java.com/
</applet>
</body>
</html>
But I would not use that. Servlets are much better idea. Applets are client side thing if you do something that do not have to be processed on back-end why not but I did not see any applets for couple of years...
If you dont see your applet then probably you have public static void main(String[] args) instead of public void init() that also could make you problems
The basic Problem about applets are that they need jvm oriented browsers to run.
due to this reason applets(java) failed. today servlets are considered. you would need eclipse or any jvm for your browser to run..

How can I run images on my Java Applet without having to sign it?

I'm new to Java programming and it seems to me like they are making this Applet signing more difficult than it needs to be...no website is straightforward on how to do it, and it gives the feeling that it would be time-consuming.
The problem is that I keep getting the "access denied" message when I try to run images on my Applet using the drawImage function.
I was told Class.getResourceAsStream would help, but I am having a hard time trying to find information on how I would implement this precisely for an image. There is also the issue that I am using NetBeans, some of this information that I find is out of my league of understanding or just doesn't fit what I need it for.
This is the kind of program I am trying to run:
import java.applet.*;
import java.awt.*;
public class JavaProject extends JApplet
{
Image img;
public void init()
{
img=getImage(getDocumentBase(),"/Image.jpg");
}
public void paint(Graphics g)
{
super.paint(g);
g.drawImage(img,20,20,this);
}
}
And this is the HTML file I am running it from:
<html>
<head>
<title> My First Web Page </title>
</head>
<body>
<applet code="JavaProject.class" width="500" height="600">
</applet>
</body>
</html>
It works with shapes just fine, but I want to implement images like this and this is where I encounter the issue. Basically, I just want to see how Class.getResourceAsStream would fit into that code there, because that code above gives me the "access denied" error.
Also, something to note is that I am running this from the file system, not from HTTP.
If you're using Java 7 you need to sign applets now:
Most significant is a new requirement that all Java applets and Web
Start Applications using the Java plug-in to run in browsers be signed
with a trusted certificate for the best user experience. Java
supports code signing, but until Java SE 7u21 it was an optional
feature. Application code signing provides numerous security benefits
to users.
http://www.oracle.com/technetwork/java/javase/tech/java-code-signing-1915323.html
However, you can change the security settings through the Java Control Panel. (On Windows just Start > Java > Configure Java.) In the Java Control Panel, go to the security tab and set the Security Level to the minimal (medium). I believe this allows you to run an applet after only a security prompt.

How to create a custom language parser for NetBeans?

I made a custom interpreter to write server-side code in javascript embedded in HTML, much like PHP, except for the actual programming language. It is implemented as a Servlet that parses Javascript using Rhino.
The pages are defined as:
<%
// Javascript code
var world = "World";
%>
<html>
<body>
<!-- the following code is a javascript expression -->
Hello <%= world %>
</body>
</html>
How could I build support for this language into NetBeans? I see that NetBeans already parses HTML and Javascript perfectly. Is it possible to reuse/combine Netbeans' HTML+JS functionality to parse & highlight these pages?
I would refer to the Netbeans WIKI "How to create support for a new language" here:
http://wiki.netbeans.org/How_to_create_support_for_a_new_language
Hope that helps :)

Java: How to invoke code running on a server from a browser?

Is there somehow that I can invoke Java running on a server from a web browser? I would like:
User navigates to URL in a browser
User fills in input boxes (text)
User presses submit button
Input fields are sent as parameters to java that is executing on the server
A new html page is displayed that was generated by the java running on the server.
What is the standard way to do this, or something similar to this.
I think with PHP this would be relatively simple. I think that you would just pass arguments after the URL like this: www.mysite.com/folder?arguments.
Yes, this is possible (and is extremely common). Two of the most common ways are Java Servlets (where responses are generated purely via Java code) and Java Server Pages (where server logic is intermingled within HTML, similar to ASP or PHP).
There are countless ways to serve HTML from Java but virtually all of them rely on java servlets and java server pages (JSPs) which are Java's specification for handling web requests.
The absolute bare minimum to get going:
Install Java EE SDK ensuring to also install Netbeans and Glassfish.
Launch Netbeans and create a "Java Web" / "Web Application" project
Enter a project name, e.g. MyWebApp
In the Server and Settings screen, you need to Add... your server so do so. Point to the file location of your Glassfish server and enter the admin name and password
Ignore the framework stuff and Finish
NetBeans will generate a sample app and you can click straightaway on Run Main Project. It will deploy your app to Glassfish and load http://localhost:8080/MyWebApp/ from your default browser
Important things to note:
A file called web.xml tells the host server some basics about your web app. This file can contain a lot of other stuff but the default is some boiler plate. The most interesting part says <welcome-file>index.jsp</welcome-file> which means when you load http://localhost:8080/MyWebApp/ it will default to load index.jsp.
The index.jsp is what gets loaded if you don't specify a page to the server. If you look at index.jsp it's just HTML with some JSP markup.
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
Creating new JSPs is as simple as writing HTML. Netbeans has a wizard to create a simple JSP.
You can embed chunks of Java into a .jsp easily and step in and out of Java / HTML with the <% %> notation such as
<%
for (int i = 0; i < 10; i++) {
%>
Hello <%=i%>
<% } %>
Glassfish is just one possible app server. As long as you write compliant code it should functional with only minimal or zero modifications on any other implementation of Java Servlet / JSP spec. e.g. Jetty, Tomcat, oc4j, JBoss, WebSphere etc.
This is just the tip of the iceberg. You can make things as simple or complex as you like.
Once you know the basics then it's up to you how deep you go. More advanced topics would be:
Taglibraries - these can remove a lot of java clutter and are considered more correct
Expressions - using expressions inside JSP pages to reduce the need for messy <%= notation
Custom servlets let you move model / business stuff into a Java class and leave the .jsp to just presentational
MVC web frameworks like Struts, Spring etc.
Security & filtering
It's a massive subject but it's fairly easy to do something quick and dirty.
As a followup to Mark Peters answer, you need a java web server like Tomcat or GlassFish in order to use servlets or jsps. There are a lot of great frameworks for Java that help you abstract away from the original servlet classes, but I'll let you look those up and decided if you even need them for something this simple.
If you want to pass arguments in a URL, then easier approach is Axis
You can display result with javascript on your page.
If you want to pass arguments in a URL, then easier approach is Axis
My school has an apache server that we are required to use. I was not allowed to install tomcat. I ended up invoking my server side Java using PHP. Not the most beautiful solution but it works.

Categories

Resources