Java -> including robot class in eclipse - java

I would like to work with this robot class :
http://docs.oracle.com/javase/1.5.0/docs/api/java/awt/Robot.html
for generating native system input´s, thats why i created this file:
import java.awt.Robot;
public class MouseClass {
public static void main(String[] args) throws Exception {
Robot robot = new Robot();
// SET THE MOUSE X Y POSITION
robot.mouseMove(300, 550);
}
}
but i got this error-msg:
Exception in thread "main" java.lang.NoClassDefFoundError: robot/robot_v1
Caused by: java.lang.ClassNotFoundException: robot.robot_v1
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
I thought, the awt.robot is already integrated in eclipse? ok if not..i dont know where to download this robot-class, the only page i found is:
http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/awt/Robot.java
but thats "Robot.java" and the error-message doent change when i include this in my "src".
Any advices how i can use this robot-class?
SOLVED ,but:
i get a compilation problem
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
at robot.robot_v1.main(robot_v1.java:5)
For this line:
public static void main(String[] args) throws Exception {
do i have to change name´s here as well in this case?
greetings
=> SOLDED with package robot;

Your issue is not related to Robot class at all. There is mismatch between your java file name and class name.
Your java class as you meantion in comment is robot/robot_v1.java and your class name is MouseClass. To make it work, all what you need to do is change your class name to be same as file which will be robot_v1 and as it is not in default package, you need to add at beginning of your file package definition, which will be for this case package robot;

Related

SlickUtil - NoClassDefFoundError when accessing API

When I try to do anything with SlickUtil, I get this error
Exception in thread "main" java.lang.NoClassDefFoundError: org/newdawn/slick/opengl/renderer/Renderer
at org.newdawn.slick.TrueTypeFont.<clinit>(TrueTypeFont.java:28)
at Text.init(Text.java:14)
at Main.main(Main.java:78)
Caused by: java.lang.ClassNotFoundException: org.newdawn.slick.opengl.renderer.Renderer
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 3 more
Code
public static Font font;
public static TrueTypeFont uf;
public static void main (String[] args) {
font = new Font("Times New Roman", Font.BOLD, 20);
uf = new TrueTypeFont(font, true); // Throws the error
}
public static void render (String text) {
uf.drawString(0, 0, text);
}
Why is this error being thrown, and what can I do about it?
Otherwise, is there a method to display 2D text with OpenGL without using SlickUtil?
Why is this error being thrown, and what can I do about it?
It is being thrown because the JAR file containing that dependency is not on the runtime classpath. (Apparently, the JAR file was available when your application was compiled. If it wasn't, then the source code that refered to the class would not have compiled.)
What you need to do is to find / identify the JAR file, and add it to the runtime classpath of your application.
Is there a method to display 2D text with OpenGL without using SlickUtil?
That is a request for us to find or recommend software for you. It is off-topic.
And moot ... if you sort out the real problem with your classpaths.

"Error: Could not find or load main class ..."

import java.util.Scanner;
import java.io.*;
import java.io.FileNotFoundException;
public class bookreader {
public static void main(String[] args) throws FileNotFoundException {
//...
}
}
When I run this code normally it spits out:
Exception in thread "main" java.lang.ClassNotFoundException: bookreader
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:122)
When I debug the code it spits out:
Error: Could not find or load main class bookreader
I'm pretty sure both errors are the same, but I don't know how to fix either of them. Other answer point towards it running as a wrong file type, but the name of the file is bookreader.java which should run it in Java like needed in Intellij.
Try the down-to-up method: Code a simple "Hello world" class, with no packages and no imports, and run it from the IDE. If it compiles well but does not run, either the project is not OK, or either the IDE is not OK.
Instead, if it runs, add more logic to this dummy class to make it resemble your initial class, repeating the test after every edit. When it fails, you'll know what was the last edit.
Make sure you are calling the class/Running the program like this :
"java bookreader" and NOT like "java bookreader.class"

ClassNotFoundException when running in Command Prompt

I am very new to java and tried to run a simple code of calculating volume. The code is below:
package chapter6;
class Box {
double width;
double height;
double depth;
}
package chapter6;
public class BoxDemo {
public static void main(String[] args) {
Box myBox = new Box();
double vol;
myBox.depth = 1;
myBox.height = 2;
myBox.width = 3;
vol = myBox.depth * myBox.height * myBox.width ;
System.out.println("Volume: " + vol);
}
}
I am able to run the code from eclipse, but when i try to run the code in Command Prompt i get the error:
C:\Prabhjot\Java\CompleteRefence\build\classes>java BoxDemo.class
Exception in thread "main" java.lang.NoClassDefFoundError: BoxDemo/class
Caused by: java.lang.ClassNotFoundException: BoxDemo.class
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
Could not find the main class: BoxDemo.class. Program will exit.
Please assist.
First class file should be at this location:
C:\Prabhjot\Java\CompleteRefence\build\classes\chapter6\BoxDemo.class
Then you should be inside:
C:\Prabhjot\Java\CompleteRefence\build\classes>
Then issue the command:
java chapter6.BoxDemo
You have put your class in a package called chapter6. This means that the java file should be in a folder called chapter6 in the class root folder of your project. And when you run it, you should be in the root folder and use the command java chapter6.BoxDemo
There is mistake in how you are running your program from console.
You are doing
java BoxDemo.class
But you need to do only
java BoxDemo
While running your program you don't need to mention .class with the name.
and if you are accessing it from root folder then you need to do
java chapter6.BoxDemo
try this
C:\Prabhjot\Java\CompleteRefence\build\classes>java chapter6.BoxDemo (RUN)
There is no need to specify .class extinction to the file while running.After compiling the java file it will create the .class file.
EXAMPLE
When you invoke BoxDemo.class, Java looks for a class called class in the BoxDemo package, which doesn't exist. As you can see from the output java.lang.NoClassDefFoundError: BoxDemo/class, it's searching for a directory BoxDemo.
Instead, just specify the class name: BoxDemo; e.g. java BoxDemo.

When running Eclipse, I am getting a error in console after running the code

So am literally taking a intro to Java Programming and now I am trying to create my second assignment. Upon doing so I have created the new java class within eclipse, but when I try to run the code that I have written, I am getting the following errors.
I am not real sure on how to fix this. Any assistance would be greatly appreciated.
Exception in thread "main" java.lang.NoClassDefFoundError: math
Caused by: java.lang.ClassNotFoundException: math
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
ClassNotFoundException: math
I'm guessing you created a class with a capital M in math, but when you call it somewhere, it's not capitalized, or referenced properly. Try to double check that in your code.
Friendly FYI for proper Java standards, try to make sure that classes are capitalized.
You said your code is the following:
public class rounding {
public static void main (string[] args){
System.outprintln("Hello World");
From the error message you received, you most likely have this code in a file named math.java. In Java, you must declare the class name as equal to the file name; for example, YourClass.java would declare public class YourClass {.
So what you need to do is either rename your class name to math, or name your file rounding.java, although it is unconventional to begin class names with a lower case letter.
On top of these errors however, you'll run into an error saying that the type string does not exist. You need to rename that the type string[] to String[] or the JVM won't be able to identify your main method.
You will also receive an error saying that outprintln is not a method of the System class. To print, use System.out.println("Your message") rather than System.outprintln("Your message").
It sounds like you're trying to access a class called "math" that does not exist in your current project or scope. Did you forget to import a particular library that you're supposed to be using?
This is the correct code:
public class Rounding {
public static void main (String[] args){
System.out.println("Hello World");
}
}
Some minor bugs which is usually done by all who start out with Java and Eclipse. The description of the error has already been given by Vulcan.

OpenGL in Java on Netbeans: NoClassDefFoundError

I am on a Mac running Netbeans 6.9.
I downloaded and installed LWJGL using this tutorial down to the letter:
http://lwjgl.org/wiki/index.php?title=Setting_Up_LWJGL_with_NetBeans
I finished the installation and copied sample code to see if my system is working. I got a bug, and was not sure if it was because of faulty code or I was doing something wrong. So I shortened down the code to this little simple bit:
package javaopengl;
import org.lwjgl.Sys;
import org.lwjgl.opengl.Display;
//Testing
public class Main {
public static void main(String[] args) {
boolean fullscreen = (args.length == 1 && args[0].equals("-fullscreen"));
try {
Display.create();
Display.destroy();
} catch (Exception e) {
e.printStackTrace(System.err);
}
System.exit(0);
}
}
But I still get the same error:
run:
Exception in thread "main" java.lang.NoClassDefFoundError: =
Caused by: java.lang.ClassNotFoundException: =
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
I am not sure what exactly is going on, Would you please tell me what is going on and how to fix it?
Note: When i am looking at the text in the development environment, it does not show those red lines indicating there are any errors.
What are you typing (or what is netbeans running) to run this? Since the Mac filesystem is fairly case-agnostic unless you've specified otherwise, running java javaopengl.main will look for a file main.java, which is there (Main.java will be returned). But the class is Main, and you can get this exception from the difference. If this is being run from an ant script, I suggest making sure you have the correct capitalization (the class should be javaopengl.Main). A simple way to test this is to delete everything except the class definition and an empty public static void main(String[] args) {}
Alternatively, you could have something simpler, like your classpath out of whack. Missing the lwjgl jar would get you there, but if you followed the directions in that tutorial, that actually seems less likely. Still, you can test this.
package javaopengl;
public class Main {
public static void main(String[] args) {
System.out.println("well, main works");
Class checkjar = Class.forName("org.lwjgl.opengl.Display");
System.out.println("My ClassLoader found: " + checkjar.getCanonicalName());
}
}
Also, remove import org.lwjgl.Sys; from your shortened example. It doesn't appear to be needed, provided it isn't the source of your problems :).

Categories

Resources