donot understand class not found exception [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I am creating a login page for a Library Management System.In the below piece of code i am getting a class not found exception.But i donot understand which class is it talking about and why is this exception taking place.Moreover the HomeStudent class is already defined and all the classes in the same folder in the workspace.
import java.awt.Button;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MainClass extends Frame implements ActionListener {
static MainClass instance ;
Panel p = new Panel(new FlowLayout());
Label uname = new Label("UserName");
Label pass = new Label("Password");
TextField tuname = new TextField(10);
TextField tpass = new TextField(10);
Button login = new Button("GO");
public MainClass()
{
p.add(uname);
p.add(tuname);
p.add(pass);
p.add(tpass);
p.add(login);
login.addActionListener(this);
}
public static void main(String[] args)
{
instance = new MainClass();
instance.setVisible(true);
instance.setSize(new Dimension(100,100));
}
#Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
//DataBase Part
//Presently Done Simply
//Simple If Else
//Onlu Used for Prototyping
if(tuname.getText().equals("user")&&(tpass.getText().equals("user")))
{
HomeStudent home = new HomeStudent();
instance.setVisible(false);
home.setVisible(true);
home.setSize(new Dimension(100,100));
}
}
}
Below is the stack trace.
load: class .class not found.
java.lang.ClassNotFoundException: .class
at sun.applet.AppletClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.applet.AppletClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.applet.AppletClassLoader.loadCode(Unknown Source)
at sun.applet.AppletPanel.createApplet(Unknown Source)
at sun.applet.AppletPanel.runLoader(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

It's talking about your main method.
public static void main(String args)
the String parameter MUST be a String array of args. It is what the main must take, and it is not, so that is where your error is. Just change to
public static void main(String[] args)
and you should be all set.
Hope that helps!

Related

Java FX Exception (Media Player Application)

I'm just starting to use Java FX and cannot for the life of me figure out why this exception keeps occurring.
I have 2 classes. One called Main and the other called Player. Here is what they each look like....
Main Class
package application;
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.paint.Color;
public class Main extends Application {
#Override
public void start(Stage primaryStage) {
Player player = new Player("/Applications/Nacho.mp4");
Scene scene = new Scene (player, 720, 480, Color.BLACK);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Player Class
package application;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.Pane;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.media.MediaView;
public class Player extends BorderPane {
Media media;
MediaPlayer player;
MediaView view;
Pane nPane;
public Player(String file){
media = new Media(file);
player = new MediaPlayer(media);
view = new MediaView(player);
nPane = new Pane();
nPane.getChildren().add(view);
setCenter(nPane);
player.play();
}
}
Here is what the compiler spits out when I run the program...
START
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$152(LauncherImpl.java:182)
at com.sun.javafx.application.LauncherImpl$$Lambda$51/1323468230.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalArgumentException: uri.getScheme() == null! uri == 'Applications/Nacho.mp4'
at com.sun.media.jfxmedia.locator.Locator.(Locator.java:211)
at javafx.scene.media.Media.(Media.java:391)
at application.Player.(Player.java:16)
at application.Main.start(Main.java:13)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(LauncherImpl.java:863)
at com.sun.javafx.application.LauncherImpl$$Lambda$54/1046396900.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/186276003.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$170(PlatformImpl.java:295)
at com.sun.javafx.application.PlatformImpl$$Lambda$49/287662571.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(PlatformImpl.java:294)
at com.sun.javafx.application.PlatformImpl$$Lambda$48/237061348.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Exception running application application.Main
FINISH
I know that the issue occurs as soon as the Player object is instantiated in Main. As soon the Player object uses it's constructor and creates a Media object, which gets passed the String file location, that's when I get the error. I ran the program with an empty constructor for the Player object and it ran fine so the issue is clearly in my Player Class, again when the Media object is instantiated. Am I passing the correct parameters? "/Applications/Nacho.mp4"
If someone can please tell me how to fix this, I would greatly appreciate it. I literally can't move forward with this project otherwise. Thank you!
Nevermind, I figured it out, the problem was passing the parameter to the Media object. I'm used to C++ syntax and did not realize that the proper way to do it is
media = new Media("file:///Applications/Nacho.mp4");
This error usually occurs due to wrongly input of the file address. The correct way to write the address is file:///C:/abc.mp4.
More clearly:
Object b = new Object("file:///C:/abc.mp4");

Scala Script Engine creating another instance of a singleton

So, I'm creating a scripting system using JSR 223 for Scala, but I came across this problem I can't find any cause for.
There is a singleton-like class, which has methods to add event listeners (from the scripts) and dispatch events (from the core). Everything worked fine, but for some reason the added listeners had disappeared when I got to dispatch an event.
After reproducing the problem, I found out that the script engine creates another instance of the singleton:
Here is my Singleton class:
package test;
import java.util.Arrays;
public final class Singleton {
private static final Singleton instance = new Singleton();
private Singleton() {
Arrays.stream(Thread.currentThread().getStackTrace()).forEach(System.out::println);
System.out.println();
}
public static Singleton instance() {
return instance;
}
}
And here is my Main class:
package test;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public final class Main {
public static void main(String[] args) throws ScriptException {
ScriptEngine engine = new ScriptEngineManager().getEngineByName("scala");
// this is a trick I found to access the classpath,
// might be the problem
#SuppressWarnings("rawtypes")
scala.collection.immutable.List nil = scala.collection.immutable.Nil$.MODULE$;
#SuppressWarnings("unchecked")
scala.collection.immutable.$colon$colon<String> vals = scala.collection.immutable.$colon$colon$.MODULE$.apply("true", nil);
((scala.tools.nsc.interpreter.IMain) engine).settings().usejavacp().tryToSet(vals);
engine.eval("test.Singleton.instance");
Singleton.instance();
}
}
And here is the output:
java.lang.Thread.getStackTrace(Unknown Source)
test.Singleton.<init>(Singleton.java:10)
test.Singleton.<clinit>(Singleton.java:7)
$line3.$read$$iw$$iw$.<init>(<console>:8)
$line3.$read$$iw$$iw$.<clinit>(<console>)
$line3.$eval$.$result$lzycompute(<console>:5)
$line3.$eval$.$result(<console>:5)
$line3.$eval.$result(<console>)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
scala.tools.nsc.interpreter.IMain$ReadEvalPrint.call(IMain.scala:773)
scala.tools.nsc.interpreter.IMain$ReadEvalPrint.callEither(IMain.scala:777)
scala.tools.nsc.interpreter.IMain$ReadEvalPrint.evalEither(IMain.scala:792)
scala.tools.nsc.interpreter.IMain$WrappedRequest.eval(IMain.scala:613)
scala.tools.nsc.interpreter.IMain.eval(IMain.scala:1047)
javax.script.AbstractScriptEngine.eval(Unknown Source)
test.Main.main(Main.java:19)
java.lang.Thread.getStackTrace(Unknown Source)
test.Singleton.<init>(Singleton.java:10)
test.Singleton.<clinit>(Singleton.java:7)
test.Main.main(Main.java:20)
The stack trace shows that the script engine ends up creating a new instance of Singleton, but I got no idea why.
Thank you.
Found out the fix after browsing the source code:
((scala.tools.nsc.interpreter.IMain) engine).settings().embeddedDefaults(Main.class.getClassLoader());
This changes the ClassLoader for the ScriptEngine to the same one.

Why the "+" operator is undefined? JAVA [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I made this code in eclipse=
Strart CLASS(i made a mistace)
import javax.swing.JFrame;
public class strart {
public static void main(String args[])
{
Window object = new Window();
object.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
object.setSize(300,250);
object.setVisible(true);
}
}
Window CLASS
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.TextField;
import java.awt.FlowLayout;
import java.awt.TextField;
import java.awt.Event.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Window extends JFrame {
TextField kimeno = new TextField(25);
TextField dkimeno = new TextField(25);
TextField n1 = kimeno;
TextField n2 = dkimeno;
private JButton plus;
public Window()
{
super("Math engine");
setLayout(new FlowLayout());
plus = new JButton("+");
plus.setFocusable(false);
plus.addActionListener(new EnableButton());
add(plus);
add(kimeno);
add(dkimeno);
}
public class EnableButton implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.out.println(n1+n2);
}
}
}
But i am still getting an error in line 32 of the Window class
the error is "The operator + is undefined for the argument type(s) java.awt.TextField, java.awt.TextField"
The + operator makes no sense for TextField objects. You want
System.out.println(n1.getText() + n2.getText());
What would it mean to add two TextField Objects? The + operator in Java can only be used with operands that are numbers or strings. (Well, that's simplifying it a bit you can look up the exact rules in the Java language specification.)
If you want to calculate the sum of two numeric strings stored in the TextFields (I assume integers) you can use something like this:
Integer.parseInt(n1.getText()) + Integer.parseInt(n2.getText())
Of course, you should add some error handling.
Have you tried
System.out.println(n1.getText() + n2.getText());
The '+' opperator when used with Strings will concatenate them together.
In this case you are not dealing with Strings, but TextField's, which have the .getText() function attached to get the String representation of their value.

How to add an object from another class [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I've been using StackOverflow for a while now but this is my first question, so please exchuse me if something comes or seems not up to par.
I'd like to create a JPanel object (here referred to as 'panel') in a different class (ClassFrom) and then have it show in another JFrame object (here referred to as 'frame') in another class (ClassTo) but there seems to be something off with what I have so far since the JPanel 'panel' won't show in the JFrame 'frame' when the JLabel is clicked.
Could anyone out there please look at my code and please help me out where possible. I would like Will really appreciate.
Heres my code.
import javax.swing.JFrame;
// The Class with the JFame that gets the components from ClassFrom
public class ClassTo {
JFrame frame = new JFrame("The JFrame");
ClassFrom classFrom = new ClassFrom();
public static void main (String[] args)
{
// This is where there seems to be a problem
frame.add(classFrom.contentMethod());
}
}
import javax.swing.JLabel;
import javax.swing.JPanel;
// The Class with the components to be added to the JFrame in ClassTo
public class ClassFrom {
public static void contentMethod() {
JPanel panel = new JPanel();
JLabel label = new JLabel("Try Label");
panel.add(label);
}
}
The problem is with a static method trying to access instance variables. Declare frame and classFrom as static or in your main method to solve the compilation error.
Edit: As Ingo pointed out change the return type of contentMethod to JPanel and return that panel so it can be added to the frame variable.

type cannot be resolved [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
heres my code:
import javax.swing.*;
import java.awt.*;
public class FirstGui extends JFrame {
private JLabel label;
private JButton button;
public FirstGui() {
setLayout(new FlowLayout());
button = new JButton("Click for sex");
add(button);
label = new JLabel("");
add(label);
event e = new event();
button.addActionListener(e);
}
public class event implements ActionListener {
public void actionPerformed(ActionEvent e) {
label.setText("how you can see wors here");
}
}
public static void main(String [] args) {
FirstGui gui = new FirstGui();
gui.setDefaultCloseOperation(EXIT_ON_CLOSE);
gui.setSize(200, 200);
gui.setTitle("Title");
gui.setVisible(true);
}
}
And it generates a errors:
ActionEvent cannot be resolved to a type FirstGui.java /Test/src line 26 Java Problem
ActionListener cannot be resolved to a type FirstGui.java /Test/src line 24 Java Problem
The method addActionListener(ActionListener) in the type AbstractButton is not applicable for the arguments (FirstGui.event) FirstGui.java /Test/src line 21 Java Problem
what is wrong with it??? im new to java.
Import the following:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
ActionEvent and ActionListener are located into the java.awt.event package.
Importing java.awt.* is not enough.
Both of these classes require you to import them. You can do so by importing everything in java.awt.event:
import java.awt.event.*;
or you may just want to import specifically what you're using:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
Remember that it's considered good practice to import individual classes (the latter option) instead of importing whole packages.
If you ever get stuck like this again, looking at The Docs for any Java Class will tell you exactly what you need to import with a little diagram that looks like this:
java.lang.Object
java.util.EventObject
java.awt.AWTEvent
java.awt.event.ActionEvent

Categories