i am learning java at this moment and i've read somewhere that i can also run my java codes in a browser with an applet. if i try to use it, it says error click here for details.
the problem is that the java program does work in command prompt but not in the browser.
here is my HTML code. (Planet.class and the html file are both on my desktop)
<applet code="Planet.class" width=500 height=500 />
this is my java code (this code works nice in command prompt; don't try to get what it's exactly doing it just makes a rhombus out of *'s.) :
class HelloWorldApp {
public static void main(String[] args) {
int n=20;
int i;
int k;
int j;
for (i=1;i<=n;i++)
{
for (k=n-i;k>=0;k--)
{ System.out.printf(" ");}
for (j=1;j<=2*i-1;j++)
{System.out.printf("*");}
System.out.printf("\n");
}
for(i=n-1;i>=1;i--)
{
for (k=0;k<(n-i)+1;k++)
{ System.out.printf(" ");}
for(j=2*i-1;j>=1;j--)
{System.out.printf("*");}
System.out.printf("\n");
}
}
}
i believe that it is just because the printf isn't supported in the browser but maybe i'm doing something else totally wrong please tell me.
It is not an applet. For this class to be an applet it would have to inherrit either Applet or JApplet.
Applets don't use main method (they have other lifecycle methods)
You were right --- system out does not end in a webpage but in java console.
Please read http://docs.oracle.com/javase/tutorial/deployment/applet/index.html
Your class must extend JApplet. If you use NetBeans, you will get some generated code and you can start learning from there.
Related
I'm having a problem accessing my Java Applet from my app. The app itself is a Cordova iOS app and therefor written in JavaScript/HTML/CSS.
I read some tutorials about how to implement an Applet so I started my first steps by copy&paste one of the "examples" I found.
So I added to my index.html:
<body><applet id="socketApplet" width="0" height="0" archive="applet/socketApplet.jar" name="socketApplet" code="socketApplet" scriptable="true"></body>
and in my JavaScript I'm trying to access it via:
document.socketApplet.testApplet();
or
document.getElementById("socketApplet").testApplet();
but none of them works.
//edit: Worth noting that the Applet itself runs fine in Eclipse.
I debugged it with Safari Developer Web Information which states:
TypeError: 'undefined' is not a function (evaluating 'document.getElementById("socketApplet").testApplet()')
For me that means it can't find the applet file, so I moved it around a bit (e.g. in the index.html directory) to make sure it's not just a real "find it" problem but it didn't help.
For informational purposes:
Using a MacBook Pro with version 10.9.3
An iPad4 with iOS 6.1.3 (Testing reasons)
Writing the Applet with Eclipse Keplar (pasted it in there though)
Compiled it as a runnable jar file.
Xcode 5.1.1
The App itself runs fine, it's only the applet which I can't get to work.
Applet code:
import java.applet.*;
public class socketApplet extends Applet {
public void init() {
System.out.println("Applet initialisiert.");
}
public void start() {
System.out.println("Applet gestartet.");
}
public void paint() {
System.out.println("Applet aktualisiert.");
}
public void stop() {
System.out.println("Applet angehalten.");
}
public void destroy() {
System.out.println("Applet beendet.");
}
public String testApplet() {
System.out.println("Applet getestet.");
return "Yep, I'm the Applet.";
}
}
Edit2: When checking document.applets.length I get the value "1" so something is there.
And when referring to the applet like document.applets[0].code I still get a response ("socketApplet" in this case). Trying document.applets[0].testApplet() however still does nothing. Function returns a String which I print out so if it works there should be something.
I am using eclipse for long time and this never happened but I have this really simple program . And it doesnt display anything . what can be the reason ?
public class ReportGenerator {
public static void main(String[] args){
System.out.println("STARTING");
}
}
try Window -> Show View -> Console if the console is not visible
If you are doing javac ReportGenerator.java, it is just building a .class file. You need to run java ReportGenerator to see the program working and therefore printing on the terminal in your case.
Too many opened consoles in Eclipse?
Here is the reference: http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Freference%2Fviews%2Fconsole%2Fref-open_action.htm
Click the grey X until all of them are closed.
I'm trying to open the On Screen Keyboard from within a web application on a touch screen interface. I am using Opera as the browser for the built in "Kiosk" features, but it does not support VBScript- an easy way of openning a .exe file from a webpage.
I know Java can be ran from within a web page and it can also be used to open another application, such as the OSK!
Below is my working Java code:
package runtimeexec;
import java.io.IOException;
public class RuntimeExec {
public static void main(String[] args) {
try {
Runtime runTime = Runtime.getRuntime();
Process process = runTime.exec("cmd /c osk");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Now I want to embed this into a webpage, so that it can open the osk. My .class file is at http://theyconfuse.me/java/runtimeexec/RuntimeExec.class and my current attempt to embed this code is at http://theyconfuse.me/java/ with the following embed code:
<applet codebase="http://theyconfuse.me/java/runtimeexec" code="RuntimeExec.class" width="200" height="200"></applet>
How ever, when I load the page, I get the following:
NoClassDefFoundError
RuntimeExec (wrong name: runtimeexec/RuntimeExec)
Can anyone help me with what am I missing here? Thanks
code="RuntimeExec.class"
Should be with package structure, separated by dots, but without the .class extension.
code="runtimeexec.RuntimeExec"
I'm recently starting to learn Java and have had success writing and compiling my own application (written in Sublime Text, a text editor, and compiled via javac).
The application runs perfectly when launched via a terminal (or command prompt if I'm on my Windows PC), but if I try launching it from the file itself (in Windows, double clicking it and ensuring Java is the open-with method, or on my Ubuntu laptop, making it executable and doing the same) I get a very short lived loading cursor and then nothing.
The application (which converts between degrees Celsius and Fahrenheit) uses some simple Swing dialogs to get the user's input and to display the result.
import javax.swing.*;
public class DegreesConversion
{
public static void main( String [] args)
{
String input = JOptionPane.showInputDialog("Enter a temperature followed by either C for Celcius or F for Fahrenheit\nE.g. 30C or 86F");
int degrees = Integer.parseInt(input.substring(0,input.length()-1));
switch (input.toLowerCase().contains("f") ? 0 : input.toLowerCase().contains("c") ? 1 : 2){
case 1:
JOptionPane.showMessageDialog(null,(((degrees*9)/5)+32)+" degrees Fahrenheit", "Conversion complete", JOptionPane.INFORMATION_MESSAGE);
break;
case 0:
JOptionPane.showMessageDialog(null,(((degrees-32)*5)/9)+" degrees Celcius","Conversion complete",JOptionPane.INFORMATION_MESSAGE);
break;
default:
JOptionPane.showMessageDialog(null, "The input you entered was not recognised!","Unknown input", JOptionPane.ERROR_MESSAGE);
break;
}
}
}
(This is by no means meant to be a serious or terribly functional application, it was simply my own attempt at making something in Java)
Anyhow, I'm not sure why this application, when compiled, only functions when launched from a CLI using "java DegreesConversion", and not when launched through a double click. I have looked for answers regarding this on Google and Stackoverflow, but haven't found anything near a relevant solution or hint as to why this is so.
I'm considering that Java .class files can't be executed the same as .jars?
Any assistance would be greatly appreciated!
Package your class into a jar file and add a manifest to it pointing to the class having the main method you like to execute.
Then you can do java -jar jarFile.jar from terminal but it is also possible to easily to run it through, e.g., Windows Open-With capabilities.
EDIT:
JAR tutorial
I'm brand new to java and have tried to get this to work for the last 48 hours and I'm about to give up.
I want to put the java applet on a website. Works fine only in eclipse. I tried many solutions already suggested on this site and none of them worked for me and just mucked up the code so I've reverted it to my original state. Could anyone pinpoint the problem? Thanks!
(code edited to reflect suggested answers)
package nameapp;
import java.util.*;
import java.io.*;
import java.applet.Applet
public class NameApp extends Applet{
public static void main(String[] args) throws IOException {
String name;
BufferedReader reader;
reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("What is your name? ");
name = reader.readLine();
if (name.equals("Mike")) {
System.out.print("Hello it's ");
System.out.println(new Date());
System.out.print("My name is ");
System.out.print(name);
System.out.println(" and I am totally awesome!!!");
}
else if (name.equals("Lisa")) {
System.out.print("Hello it's ");
System.out.println(new Date());
System.out.print("My name is ");
System.out.print(name);
System.out.println(" and I'm the prettiest gal in the world!");
}
else {
System.out.print("Hello it's ");
System.out.println(new Date());
System.out.print("My name is ");
System.out.print(name);
System.out.println(" and I'm just ok i guess...");
}
}
}
And html is...
<applet code=nameapp/NameApp.class width=300 height=300>
<param name="bgcolor" value="ffffff">
<param name="fontcolor" value="000000">
Your browser is not Java enabled.
please read this tutorial that's contains basic stuff about JApplet,
please use JApplet not Applet
carrefully read signed java applet restrictions
What Applets Can and Cannot Do
https://stackoverflow.com/tags/java-web-start/info
note with Java 1.6.025 comings another restrictions for JApplet, and these problems and possible workaround are detailed described on this forum by Andrew Thompson, but I lost link ...
It looks like you are writing an application rather than an applet. When you run it in eclipse, do you select Run As... and then select Java Application? Try running it as a Java Applet instead. You should see appletviewer pop up with no content in it.
The entry point for an applet is the init() method, not main(), and the graphics related method paint() is also usually overloaded; I haven't seen an applet that has access to standard in and out.
You might find the stackoverflow question here useful: Main vs init.
code="nameapp/NameApp.class"
Put it would also need to extend java.applet.Applet and generally be an applet.
Read the Applet tutorial - Instead of a public static void main(String[] args) method, an Applet needs public void init().
Also, with the code you have now, you'll just see a blank Applet - you'll have to have the Java Debug Console up to see anything printed by System.out.println(), and Applets can't access System.in to read input - instead you'll want to add some TextField components to your Applet and read and write the text with those.