I've been working on a project that requires communication both directions between Java and JavaScript. I have successfully managed to get it working under all browsers in OS X, but I'm now faced with the challenge of getting it to run on Windows under any browser. At the moment it simply doesn't work.
I'm just wondering if there is something special I need to do in order for JavaScript to communicate with Java?
My applet code looks like this:
<applet id='theApplet'
code="com/company/MyApplet.class"
archive="SMyApplet.jar"
height="50" width="900"
mayscript="true" scriptable="yes">
Your browser is ignoring the applet tag.
</applet>
Once the applet has loaded, I then try to call functions on it like this:
alert("Call some java:" + theApplet.testFunc());
And in the firebug console I get the following error:
theApplet.testFunc is not a function
I can confirm that this doesn't work in IE either.
When the page loads, I have the java console open and I can see that the applet is successfully loading and ready to accept calls.
Any help would be greatly appreciated!
Cheers
Update: Here is the stripped down java code exposing the public api that I'm trying to call.
package com.company;
import com.google.gson.Gson;
import java.applet.*;
import java.io.*;
import java.net.*;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.*;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.*;
import netscape.javascript.*;
public class MyApplet extends Applet implements Runnable
{
public void init()
{
JSON = new Gson();
isReadyVar = 0;
workThread = null;
}
public void start()
{
}
public void run()
{
System.out.println("Done");
}
public void stop()
{
}
public void destroy()
{
}
/* Public API */
public int testFunc()
{
return 200;
}
}
Update [SOLVED]:
I figured out what the problem was exactly. Turns out the Gson lib I was using wasn't signed; but my own jar was. Browsers on windows require that all libs are signed; so I packaged Gson in with my java files & signed the lot and it solved the problem! Thanks for everyones help!
I figured out what the problem was exactly. Turns out the Gson lib I was using wasn't signed; but my own jar was. Browsers on windows require that all libs are signed; so I packaged Gson in with my java files & signed the lot and it solved the problem! Thanks for everyones help!
alert("Call some java:" + document.getElementbyId("theApplet").testFunc());
Make sure the testFunc() method is declared as public access.
If that does not work, post the applet code as an SSCCE.
BTW
Incorrect
code="com/company/MyApplet.class"
Correct
code="com.company.MyApplet"
BTW 2
Incorrect
..scriptable="yes">
Correct
..scriptable="true">
Since the applet element is deprecated, I use following code, which works at least in Firefox:
<object id="MyApplet" classid="java:com.example.myapplet"
codetype="application/java" codebase="bin/" height="10" width="10"
</object>
Related
Here's my code
The working directory for my program is printed at the bottom along with the error message, and on the left hand side of the screen my download png is located directly within my test project where the working directory is supposed to be. I have also tried using the direct file path of my download png within the getResource() method, but that didn't work either (I used double backslashes so that wasn't the issue).
package test;
import javax.swing.ImageIcon;
import javax.swing.Icon;
import java.io.File;
public class Test{
public Test() {
System.out.println(new File(".").getAbsolutePath());
ImageIcon download = new ImageIcon(getClass().getResource("download.png"));
}
public static void main(String[] args) {
new Test();
}
}
(Yes I know I didn't have to import Icon but I wanted to match what I had in the picture)
All I had to do was put the download.pgn file in the package where my class was located rather than the project that the absolute path was telling me to put it and the code worked. Hopefully this helps others who are having the same problem
I am developing a simple calculator application in Java 8/9 in Eclipse. I am working on the power operation (as in "to the power of" used in math). I want to use the Math.power() instead of a for loop. However, I am having trouble importing the java math package into the program. The internet says to add import java.lang.math. When I try to code it in, I receive a notice of "Cannot Perform Operation. This compilation unit is not on the build path of the Java Project". What am I overlooking and/or doing wrong? Please provide suggestions or feedback.
Please note: Yes this is an academic assignment. To make this clear, I am not asking for the coding of the power operation. This issue is specifically about the importing the math package.
power operation (power.java)
package org.eclipse.example.calc.internal.operations;
import org.eclipse.example.calc.BinaryOperation;
// import java.lang.math; produces error
// Binary Power operation
public class Power extends AbstractOperation implements BinaryOperation {
// code removed. not relevant to SOF question.
}
Main (calculator.java)
package org.eclipse.example.calc.internal;
import org.eclipse.example.calc.BinaryOperation;
import org.eclipse.example.calc.Operation;
import org.eclipse.example.calc.Operations;
import org.eclipse.example.calc.UnaryOperation;
import org.eclipse.example.calc.internal.operations.Power;
import org.eclipse.example.calc.internal.operations.Equals;
import org.eclipse.example.calc.internal.operations.Minus;
import org.eclipse.example.calc.internal.operations.Plus;
import org.eclipse.example.calc.internal.operations.Divide;
import org.eclipse.example.calc.internal.operations.Square;
public class Calculator {
private TextProvider textProvider;
private String cmd;
private boolean clearText;
private float value;
public static String NAME = "Simple Calculator";
public Calculator(TextProvider textProvider) {
this.textProvider = textProvider;
setupDefaultOperations();
}
private void setupDefaultOperations() {
new Power();
new Equals();
new Minus();
new Plus();
new Divide();
new Square();
}
....
BTW, I use camel Case normally, but the academic project name everything including file names in standard writing format.
EDIT: After reading a response, I realized I forget to mention this. I can't get any further than typing import java., then the error pop-ups. Then I can't type the rest of the import statement
Image of package hierarchy
Your project is not configured correctly. You have no source dir at all. The src dir should be marked as source dir; right click it and tell eclipse about this, or, as it is a maven project, it's more likely a broken pom. Also, why are you using the org.eclipse package? If you work for SAP, it should be com.sap.
My error:
Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: java.lang.UnsatisfiedLinkError: org.jbox2d.common.Timer.now()D
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:130)
Caused by: java.lang.UnsatisfiedLinkError: org.jbox2d.common.Timer.now()D
at org.jbox2d.common.Timer.now(Native Method)
at org.jbox2d.common.Timer.reset(Timer.java:35)
at org.jbox2d.common.Timer.<init>(Timer.java:31)
at org.jbox2d.dynamics.World.<init>(World.java:587)
at org.jbox2d.dynamics.World.<init>(World.java:158)
at org.jbox2d.dynamics.World.<init>(World.java:154)
at org.jbox2d.dynamics.World.<init>(World.java:145)
at com.badlogic.gdx.physics.box2d.World.<init>(World.java:61)
at com.example.blockbunny.states.Play.<init>(Play.java:22)
at com.example.blockbunny.handlers.GameStateManager.getState(GameStateManager.java:36)
at com.example.blockbunny.handlers.GameStateManager.pushState(GameStateManager.java:46)
at com.example.blockbunny.handlers.GameStateManager.<init>(GameStateManager.java:20)
at com.example.blockbunny.main.Game.create(Game.java:33)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:146)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:123)
And this occurs when I do this:
world = new World(new Vector2(0, -9.81f), true);
I have looked online, and some solutions included using this:
import com.badlogic.gdx.utils.GdxNativesLoader;
GdxNativesLoader.load();
However, I don't know where to put this function, and if it even works (I tried putting it in several different places)
How can I fix this issue?
Help will be appreciated, thanks!
As requested, here are my imports:
import static com.example.blockbunny.handlers.B2DVars.PPM;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;
import com.example.blockbunny.handlers.GameStateManager;
import com.example.blockbunny.main.Game;
I used Box2d several time with libgdx and i never encountered such a problem :
here what i propose t you :
GdxNativesLoader.load();
should be put on the create() method, but that doesn't seem to work with you
also try call it in a static way like this :
static {
GdxNativesLoader.load();
}
but i thin your problem is the extension of the library that you are using
verify that you are using gdx-box2d and not gdx-box2d-gwt which is used only for Html project
Verify that you are using the right (jar file) /(extension library) : gdx-box2d.jar and gdx-box2d-natives.jar (also verify the build path)
also try :
upgrade the box2d version that you're using
those were all the arrows i had ! hope one of it will work
Good luck !!
I came across the same issue, was using the gwt libraries. Updated the Maven dependency artifact from
gdx-box2d-gwt
to
gdx-box2d
and it fixed the error.
my code is below.I try to upload image into cloudinary through java but not uploaded it shows the below error
Exception in thread "main" java.lang.UnknownError: Can't find
Cloudinary platform adapter
[com.cloudinary.android.UploaderStrategy,com.cloudinary.http42.UploaderStrategy,com.cloudinary.http43.UploaderStrategy]
at com.cloudinary.Cloudinary.loadStrategies(Cloudinary.java:76) at
com.cloudinary.Cloudinary.(Cloudinary.java:91) at
ImageUpload.main(ImageUpload.java:16)
my code is following
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import com.cloudinary.Cloudinary;
import com.cloudinary.utils.ObjectUtils;
public class ImageUpload {
public static void main(String arg[])throws Exception{
Map config = ObjectUtils.asMap(
"cloud_name", "dq8rshzka",
"api_key", "484362882976754",
"api_secret", "1zwPe6-VfVjj3rueX6zSsfyNyro");
Cloudinary cloudinary = new Cloudinary(config);
Map result = cloudinary.api().resource("sample", ObjectUtils.emptyMap());
}
}
Please open a support ticket and Cloudinary's support team will be happy to assist. Regardless, please note that your account's api_secret should never be revealed. You should go to your account's settings page and generate a new pair of api key and secret.
Try add this to your proguard rules file:
-keep class com.cloudinary.** { *; }
Here is my solution for this issue:
-keep class * extends com.cloudinary.strategies.*
this will only keep the missing classes that the SDK needs and mentioned in this error.
You need to add http cloud library,you can find at http://mvnrepository.com/artifact/com.cloudinary/cloudinary-http44/1.3.0
Since version 1.24.1 of the sdk, this issue is fixed as the sdk add the proguard rules itself. See commit https://github.com/cloudinary/cloudinary_android/commit/c8ce933d4396867a18aeb1511198f2abad065e95
I am trying to run Java applet using Google Chrome browser. Everytime I am getting no class found exception. Here is my code.
HelloWorld.java
package my.first.pack;
import java.applet.Applet;
import java.awt.*;
public class HelloWorld extends Applet {
/**
*
*/
private static final long serialVersionUID = 2741715258812838900L;
public void paint(Graphics g) {
g.drawString("welcome", 150, 150);
}
}
Hello.html
<applet code="my.first.pack.HelloWorld" width="300" height="300">
Sign your applet and all the .jar dependencies with a certificate.
Populate your manifest with all the tags mentioned below (it's in xml because I use maven, you can write in the way you prefer)
<codebase>http://location.of.your.jar/</codebase>
<permissions>all-permissions</permissions>
<Application-Library-Allowable-Codebase>http://location.of.your.jar/</Application-Library-Allowable-Codebase>
<Manifest-Version>1.0</Manifest-Version>
<Implementation-Title>App Name</Implementation-Title>
<Implementation-Version>0.1.0</Implementation-Version>
<Application-Name></Application-Name>
<Created-By>1.8.0_45</Created-By>
<Main-Class>package.YourClass</Main-Class>
<mode>development (or production)</mode>
<url>url of the application</url>
Surround your java method with the doPrivileged
Be sure that your browser has the java plugin enabled
Put your http path of your web app in the java exception list
If your url has _ (underscore/underline) probably it won't be recognized.
Try to move your .jar to the same folder of your html, not using the /applet folder.
Take a look on this post, I was having a similar issue.
Remember, this error saying that 'is not a function' is because your .jar is not loading - or you made something wrong with the js syntax, what I don't think so.