Java Code Compile Error - java

I am trying to compile a 4KB Java game called "Left 4K Dead".. anyways, It will compile successfully with the javac G.java command, but when you go to run it using java G it spits back this error at me:
Exception in thread "main" java.lang.NoSuchMethodError: main
Anyone know how to make this work? Thanks :)
Beginning of code:
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.*;
public class G extends Applet implements Runnable
{
private boolean[] k = new boolean[32767];
private int m;
public void start()
{
enableEvents(AWTEvent.KEY_EVENT_MASK | AWTEvent.MOUSE_EVENT_MASK | AWTEvent.MOUSE_MOTION_EVENT_MASK);
new Thread(this).start();
}
public void run()
{
BufferedImage image = new BufferedImage(240, 240, BufferedImage.TYPE_INT_RGB);
Graphics ogr = image.getGraphics();

Exception in thread "main" java.lang.NoSuchMethodError: main
This exception indicates exactly what it states, there is no main method, so the program cannot start.
The issue with the Left 4k Dead code is it is an applet. It expects to be compiled and then run from a webpage, not run from command line (i.e. you can't run it with the command java). If you wish to run it from the command line, you should look into a standalone applet viewer.

After compilation of the class, use
appletviewer <class-name>
to run it through the commandline since it is an applet.
OR ELSE
you can embed it in a browser as CodeMaker suggests.

Related

OpenCV - Java: VideoCapture read frame hangs with usb camera

I'm quite new to OpenCV - Java programming, and I'm trying to setup an application to read video frames from USB WebCam, to start with something.
This is the document I followed up to now: https://opencv-java-tutorials.readthedocs.io/en/latest/03-first-javafx-application-with-opencv.html#video-capturing
The setup is the following:
Java version: 10.0.1
OpenCV Version: 3.3.4 and 3.2.0, same error with both versions
OS: Windows 10 x64
The .dll is placed under C:\Windows, that is included in my java.library.path
I have some additional frameworks involved in the application, but I prepared an isolated test case to better check the issue:
import org.junit.Test;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.videoio.VideoCapture;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class CVCaptureTest {
private static final Logger LOG = LoggerFactory.getLogger(CVCaptureTest.class);
#Test
public void testFrameRead() {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
VideoCapture capture = new VideoCapture();
capture.open(0);
for (int i = 0; i < 100; i ++) {
if (capture.isOpened()) {
Mat frame = new Mat();
LOG.info("Capture open. Reading frame...");
capture.retrieve(frame);
LOG.info("Captured: {}", frame.dump());
}
}
}
}
Output:
[INFO] Running i.m.r.b.r.c.CVCaptureTest
20:15:57.757 [main] INFO i.m.r.b.r.c.CVCaptureTest - Capture open. Reading frame...
After this log line the program just hangs, without throwing any exception.
Any help on understanding the cause of the freeze is welcome.
Thanks & Regards,
Mattia!
Try to use the capture.read(frame);.

How do I load an external jar file in a Applet?

So I am working on a project in C# but in order for me to progress I need to get my jar file to run.
This is the jar file
The jar file is a game called Runescape and if I were to double click that jar file it won't do much so I was looking at this example
Applet applet = (Applet)classLoader.loadClass("client").newInstance();
applet.setStub(stub);
applet.setSize(new Dimension(763, 504));
applet.init();
applet.start();
i.add(applet);
i.pack();
i.setDefaultCloseOperation(3);
label.setVisible(false);
Which by the looks of it takes the jar file and runs it in a Applet to give the user a visual representation of the game.
And thought it could be of help.
Now since I am a C# developer with minor Java experience I find it hard to know where to start, I have IntelliJ installed and ready to create a project but what do I need to do in order to get that jar file to run in a Applet so I can later on compile that project and use it a "client" for the game?
Running an applet from an Application is actually not that easy (and not partucally recommended).
I wrote a litte test for your jar file and run into an issue which I belive is that I am missing a proper AppletStub
import javax.swing.*;
import java.applet.Applet;
import java.awt.*;
import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
class appletrunner
{
public static void main(String[] args) throws Exception //This is not proper Exception handling!
{
JFrame frame = new JFrame("test");
File jar = new File("gamepack_8614663.jar");
URLClassLoader classLoader = new URLClassLoader(new URL[] {jar.toURI().toURL()}, appletrunner.class.getClassLoader());
Class<?> client = classLoader.loadClass("client");
Applet applet = (Applet)client.newInstance();
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
applet.stop();
applet.destroy();
}
});
frame.getContentPane().add(applet);
frame.pack();
frame.setVisible(true);
applet.init(); // Exception here
applet.start();
}
}
This Fails on applet.init() with a cryptic Exception (caused by the obfuscator used by runescape jar). But I think with a proper AppletStub which itself requires a AppletContext it could work.
You could try implementing a class for these interfaces and add them to the applet with addStub(). Hope this is somewhat helpfull.

Why wont this run in BlueJ?

I am trying to do the free Stanford programming course online, but the programs wont run when executed. I believe I have successfully added the acm library to BlueJ, because when I compile the code, the imports are accepted, and it doesn't show any errors. But when I try and run the program, nothing happens, and all I see is the text, "Start: applet not initialized." Please, Please, Please help.
The code I am using in BlueJ is the following:
import acm.graphics.*;
import acm.program.*;
public class HelloProgram extends GraphicsProgram {
public void run() {
add(new GLabel("hello, world", 100, 75));
}
}
First of all you have to install the AMCInvoker extension, then you create an instance of your class HelloProgram and call the start method.

Applet doesn't run and asks for main method

I am a novice in java programming.This is my applet code for SimpleApplet.java:
import java.awt.*;
import java.applet.*;
/*
<applet code="SimpleApplet" width=200 height=60>
</applet>
*/
public class SimpleApplet extends Applet
{
public void paint(Graphics g)
{
g.drawString("A simple Applet",20,20);
}
}
I first compiled it in command prompt by javac SimpleApplet.java and then used java SimpleApplet. It throws up error that main class is not found in class SimpleApplet,please define main method.
Where am I wrong here ?
javac SimpleApplet.java
and then used
java SimpleApplet
Do instead:
javac SimpleApplet.java
appletviewer SimpleApplet.java
The trick here is that the applet viewer will read the source, and use the applet element defined in the comment to make an 'applet HTML' to run it.

Why am I getting ClassNotFoundExpection when I have properly imported said class and am looking at it in its directory?

This is my Javac compiling statement:
javac -cp
"C:\java\code\j3D\j3dcore.jar;C:\java\code\j3D\j3dutils.jar;C:\java\code\j3D\vecmath.jar"
Simple.java
compiles with no problems.
The three jar files (j3dcore, j3dutils, and vecmath) are the essential jar's for my program (or at least I am led to believe according to this official tutorial on J3D
For the record I ripped this code almost line from line from the above pdf file.
jar files are correctly located in referenced locations
When I run my Simple program, (java Simple) I am greeted with
Exception in thread "main" java.lang.NoClassDefFoundError:
javax/media/j3d/Cavas3d Caused by: java.lang.ClassNotFoundExpection:
javax.media.j3d.Canvas3D
Currently I am staring directly at this Canvas3D.class that is located within j3dcore.jar\javax\media\j3d\
wtfisthis.jpg
Here is the source code:
//First java3D Program
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.event.*;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.ColorCube;
import javax.media.j3d.*;
import javax.vecmath.*;
import java.awt.GraphicsConfiguration;
public class Simple extends Applet {
public Simple() {
setLayout(new BorderLayout());
GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
Canvas3D canvas3D = new Canvas3D(config);
add("Center", canvas3D);
BranchGroup scene = createSceneGraph();
scene.compile();
// SimpleUniverse is a Convenience Utility class
SimpleUniverse simpleU = new SimpleUniverse(canvas3D);
// This moves the ViewPlatform back a bit so the
// objects in the scene can be viewed.
simpleU.getViewingPlatform().setNominalViewingTransform();
simpleU.addBranchGraph(scene);
} // end of HelloJava3Da (constructor)
public BranchGroup createSceneGraph() {
// Create the root of the branch graph
BranchGroup objRoot = new BranchGroup();
// Create a simple shape leaf node, add it to the scene graph.
// ColorCube is a Convenience Utility class
objRoot.addChild(new ColorCube(0.4));
return objRoot;
}
public static void main(String args[]){
Simple world = new Simple();
}
}
Did I import correctly?
Did I incorrectly reference my jar files in my Javac statement?
If I clearly see Canvas3D within its correct directory why cant java find it?
The first folder in both j3dcore.jar and vecmath.jar is "javax". Is the compiler getting confused?
If the compiler is getting confused how do I specify where to find that exact class when referencing it
within my source code?
try:
java -cp "C:\java\code\j3D\j3dcore.jar;C:\java\code\j3D\j3dutils.jar;C:\java\code\j3D\vecmath.jar" Simple
you need to include the classpath on the javacommandline as well.
Just doing java simple wont help. You need to put all those jars in classpath while you run the program. Just like you did to compile it.
java -cp C:\java\code\j3D\j3dcore.jar;C:\java\code\j3D\j3dutils.jar;C:\java\code\j3D\vecmath.jar Simple
Please Check Setting the class path

Categories