class not found exception java in server? - java

I have a java applet which works in eclipses, and i exported it like this, right click folder, export, export as jar, selected class files and project files and 'src' folder, then press 'next' 'next' and then i pick the main class which is called Main_run, which has a main class but don't have anything in it
public class Main_run extends Applet implements Runnable, KeyListener, MouseMotionListener, MouseListener
{
public static void main (String[] args )
{
}
}
this is because the applet start with 'init()'
Then I made a html page which has this on it
<b><applet code="Desktop.jar/Main_run.class" width="320" height="120"></b>
not (<) should be <.
then I added both file, the exported jar file and the index to the server im getting this error
"ClassNotFoundException"
Any idea how to fix it?

You might want to take a look at the question at How to embed jar in HTML.
In other words, use <applet archive="Desktop.jar" code="Main_run.class" width="320" height="120">. That's my thought on the subject.

Related

Background not showing up in java processing project

I think I'm doing everything right in my code but my background won't show up in my processing project, here is my code.
package finalproject;
import processing.core.PApplet;
import processing.core.PImage;
public class FinalProject extends PApplet {
PImage background;
PImage player;
public void setup() {
size(1360, 1080);
player = loadImage("player.png");
background = loadImage("rust.png");
}
public void draw() {
background(background);
image(player, 500, 500);
}
}
Processing expects files to be inside a data directory next to the code.
You're presumably running this from an IDE like Eclipse instead of the Processing editor, so where you put that data directory depends on how your code is setup. And you haven't posted a MCVE, so it's hard to help you with that.
But basically, you need to debug your sketch to figure out exactly where Processing is looking for the files. Then you need to move the files there. This is probably something simple like putting them inside a data directory.
If you still can't get it working, please post a MCVE along with a screenshot or a description of your directory structure.
If you are using the processing IDE than the data folder should be located in your sketch folder next to all the .pde files. Make sure that the image you are using has the same resolution as the sketch window. If you are still having issues I would recommend that you try moving your setup and draw methods out of your class and into the main processing sketch.

How do I add a java program to a weebly site?

I'm experimenting with Weebly, and I'm currently trying to add an arbitrary swing program to the Weebly editor. I have tried two approaches thus far, as shown here:
1: (Note: - replaces < and >)
-embed height=400 width=400 src="siteName/uploads/someNumbers/testapplet.class"--/embed-
2: (Same substitution as above)
-applet codebase="siteName/uploads/someNumbers" code="testapplet.ckass" width=400 height=400--/applet-
Upon publishing and viewing the page, the first one says I need a plugin to display the content, and the second one says my security preferences won't let me run java on the site, whereas I can run java pretty much everywhere else just fine.
What should I do to make this work? This could include some of the following:
Modifying my program (i.e. the java code itself)
Modifying how I upload the program (i.e. .class vs .jar)
Modifying how I display the program (i.e. the actual -applet- or -embed-)
For reference, here is the java code- just a basic JButton and JLabel, with the JLabel's value increasing upon each click of the button:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class TestApplet extends JApplet
{
static int x = 0;
static JLabel l = new JLabel(x+"");
public void init()
{
setLayout(new FlowLayout());
JButton b = new JButton("Button");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
x++;
l.setText(x+"");
}
});
add(b);
add(l);
}
}
Sign in to weebly and click on the edit button next to your site name
On the "Elements" tab on the top, go to "Multimedia" and drag a file element onto your page.
Follow directions to upload your .class file. Name should be lowercase.
Hit the publish button and go to your published page.
Remember your .class file? Right-click on it and copy the link address or link location.
Go back to editing your weebly page.
On the "Elements" tab, go down to "More" and drag in the "Custom HTML" element.
Click to edit the custom HTML element- this is where we are going to put our applet code.
If my link location to my .class file was http://www.johndoe.com/uploads/3/3/2/6/3326331/countme.class, my applet code would read:
<applet codebase="http://www.johndoe.com/uploads/3/3/2/6/3326331" code="countme.class" width=something height=something></applet>
The idea is that the codebase tells the browser where to look for a .class file and the code itself tells it whick .class it is.
10. Publish again, and your applet should appear.
I haven't tried it personally, but I had a chat with a support support staff and they affirmed this method.
All the best!

How to load image using getResources() in Java JApplet?

I'm trying to deploy a Java applet. When I run it in the Eclipse applet viewer, it loads fine (although it doesn't respond to keyboard commands- that's a question for later). After exporting it to a JAR and trying to run it in a browser, it always gives a NullPointerException on this line in one of the classes:
private static final ImageIcon NULL_IMAGE = new ImageIcon((new Object()).getClass().getResource("/tiles/blank.png"));
I tried moving the first use of this class from init() to start() with no change. Running "jar -tf applettest.jar" in the terminal window confirms that /tiles/blank.png does indeed exist in the JAR.
So what could I have missed when exporting this JAR? Should I just forget about the applet version and use JNLP?
Move your PNG image into the same package as your class, and try this:
private final ImageIcon NULL_IMAGE = new ImageIcon(getClass().getResource("/blank.png"));`
Note that NULL_IMAGE becomes private final but not private static final.

Eclipse Java Error: Cannot find or load main class

I am using Eclipse Luna and i am trying to run a simple text program but Eclipse gives the error:
Error: Could not find or load main class Main
I have no idea what's going on. I haven't found anything useful on Stack Overflow or Google. Here is my code:
public class Main {
public static void main(String[] args) {
System.out.println("This is a String");
}
}
Thank you.
Your code looking fine !
Do following steps to run your class
Select your project, go to Project section in menu bar then click clean.
In tool section click on enter code here Build Automatically.
Select your class right click on it and then select run.
In the menu Run, select Run Configurations....
In the dialog box that opens, on the left side, you are supposed to have an entry Java Application, and under it, there should be your class (and maybe other classes you worked on before). It may be named Main or Main__ here.
Select it. If it was erroneously named Main__, change that to Main in the Name: field.
Now look at the entry for Main Class. In the text field, it should say Main. If it says Main__ or anything else, change that to Main.
Apply and Run.

Adding an applet to a website

First time posting here. I made a simple calculator program using java and I am trying to put it onto my website. From what I've gathered from previous help posts is that I need to create a JApplet with all my program contents and compress it into a .jar file. Then I need to create a .JNLP file, which describes how to applet should be launched.
So here is where I am having trouble.
package calculator;
import javax.swing.JApplet;
import javax.swing.SwingUtilities;
public class CalculatorApplet extends JApplet {
public void init()
{
try
{
SwingUtilities.invokeAndWait(new Runnable()
{
public void run() {
Calculator calc = new Calculator();
add(calc);
}
});
}
catch(Exception e)
{
System.err.println("GUI creation failed");
}
}
}
It seems my applet was not constructed properly. Whenever I run it a "java.lang.reflect.InvocationTargetException" is thrown. Whenever I run my Calculator class independently from the applet it works as expected. Any ideas where the source of my error is?
I think JNLP files are used for Java Web Start. That's something you won't need with an average Java applet. Please do correct me if I'm wrong.
If you have the working .jar file, an HTML file that calls the applet will be sufficient to run the applet. Insert the code <applet width="300" height="300" archive="jar.jar" code="class.class"></applet> into an HTML file, where class.class is the class extending Applet or JApplet and jar.jar the location of the jar file. Loading the HTML file in a browser will display the applet.
Alternatively, you can use Java's Applet Viewer to open the HTML page and open the applet locally.
You can certainly use JNLP with applets.
See https://docs.oracle.com/javase/tutorial/deployment/deploymentInDepth/runAppletFunction.html and https://docs.oracle.com/javase/tutorial/deployment/deploymentInDepth/embeddingJNLPFileInWebPage.html

Categories