Cannot connect to MySQL due to incorrect time zone - java

I have made a single java app and I want to try to connect to an sql database when window opened. I have add the my-sql-java-8.0.14.jar connector but when I run the app I get an error message.
I have tried multiple times to re-connect, re-build the database and connect with new one but i still get the below error message.
Thanks in advance for your help
"Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: Cannot connect to the dbat testDBPack.MainWindow$1.windowOpened(MainWindow.java:47)
at java.awt.Window.processWindowEvent(Unknown Source)
at javax.swing.JFrame.processWindowEvent(Unknown Source)
at java.awt.Window.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.sql.SQLException: The server time zone value '????????? ??? GTB' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:73)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:76)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:835)
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:455)
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240)
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:199)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at testDBPack.MainWindow$1.windowOpened(MainWindow.java:45)
... 25 more
Caused by: com.mysql.cj.exceptions.InvalidConnectionAttributeException: The server time zone value '????????? ??? GTB' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:85)
at com.mysql.cj.util.TimeUtil.getCanonicalTimezone(TimeUtil.java:132)
at com.mysql.cj.protocol.a.NativeProtocol.configureTimezone(NativeProtocol.java:2241)
at com.mysql.cj.protocol.a.NativeProtocol.initServerSession(NativeProtocol.java:2265)
at com.mysql.cj.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:1319)
at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:966)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:825)
... 31 more
"
I have checked my credentials, i have sql running but i cannot fix the issue.
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Connection;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JSeparator;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JButton;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
#SuppressWarnings("serial")
public class MainWindow extends JFrame {
JPanel contentPane;
static Connection conn;
public MainWindow() {
setResizable(false);
setTitle("Coding Factory");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 433, 293);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
addWindowListener(new WindowAdapter() {
#Override
public void windowOpened(WindowEvent e) {
String url = "jdbc:mysql://localhost:3306/teachers";
String username = "panos123";
String password = "panagiotis";
try {
conn = DriverManager.getConnection(url,username,password);
}catch (SQLException ex) {
throw new IllegalStateException("Cannot connect to the db",ex);
}
}
});
JLabel lblNewLabel_1 = new JLabel("Quality Assistance");
lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 30));
lblNewLabel_1.setBounds(71, 32, 295, 37);
contentPane.add(lblNewLabel_1);
JButton TeachersButton = new JButton(" ");
TeachersButton.setBounds(10, 144, 46, 34);
contentPane.add(TeachersButton);
Also i have another class for the main:
package testDBPack;
import java.awt.EventQueue;
public class TeachersApp {
static MainWindow mainFrame;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
mainFrame = new MainWindow();
mainFrame.setVisible(true);
mainFrame.setLocationRelativeTo(null);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}

Time zone is not configured. Try using the following url:
jdbc:mysql://localhost/teachers?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC

Related

java exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException

I was trying my first code in java swing and got many errors. my code is:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Swinging extends JFrame
{
JTextField ans;
int count =0;
static final long serialVersionUID = 1L;
Swinging()
{
Container cp= getContentPane();
cp.setLayout(new FlowLayout());
cp.add(new JLabel("value",7));
ans=new JTextField("0",10);
cp.add(ans);
JButton inc= new JButton("increment");
cp.add(inc);
inc.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
++count;
ans.setText(count+"");
}
});
setSize(200,200);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public class Usingswing {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Swinging(); // Let the constructor do the job
}
});
}
}
and the errors are as follows:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: horizontalAlignment
at javax.swing.JLabel.checkHorizontalKey(Unknown Source)
at javax.swing.JLabel.setHorizontalAlignment(Unknown Source)
at javax.swing.JLabel.<init>(Unknown Source)
at javax.swing.JLabel.<init>(Unknown Source)
at hopeso.Swinging.<init>(Usingswing.java:16)
at hopeso.Usingswing$1.run(Usingswing.java:45)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
i tried solving my problem using the questions posted by other people but it didn't workout. please help.
The error occurs because of the following line:
cp.add(new JLabel("value",7));
You're using JLabel's constructor that receives the text and the horizontal alignment. The alignment is an int, but it has to be one of the following constants, otherwise it will throw the IllegalArgumentException:
LEFT (2)
CENTER (0)
RIGHT (4)
LEADING (10)
TRAILING (11)
These constants are defined in SwingConstants, so you can just write something like this:
cp.add(new JLabel("value", SwingConstants.CENTER));

Adding data into database table through GUI of Java.

I created a simple table in IBM DB2 named "NAMES" with a single column "FullName" with a datatype of VARCHAR(20). I've also created a GUI with a JTextfield and JButton to add data into the table through GUI. When the button is clicked, the text in the textfield will be inserted into the table NAMES. But there's an error when I click the button.
Here is the java code.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
public class FirstClass extends JFrame implements ActionListener
{
private Connection connection;
private JTextField fieldTF;
private JButton addB;
public FirstClass() throws SQLException , ClassNotFoundException
{
setDefaultCloseOperation(this.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
fieldTF = new JTextField(20);
addB = new JButton("Add");
Container cont = this.getContentPane();
cont.setLayout(new FlowLayout());
cont.add(fieldTF);
cont.add(addB);
setConnection();
addB.addActionListener(this);
pack();
validate();
setVisible(true);
}
public void setConnection() throws SQLException , ClassNotFoundException
{
Class.forName("com.ibm.db2.jcc.DB2Driver");
connection = DriverManager.getConnection("jdbc:db2://localhost:50000/COLINN","Colinn","ezioauditore");
System.out.print("Connected Succesfully");
}
public void write(String name) throws SQLException , ClassNotFoundException
{
PreparedStatement statement = null;
String query = null;
query = "INSERT INTO NAMES VALUES (?)";
statement.setString(1,name);
statement.executeUpdate();
}
public void actionPerformed(ActionEvent e)
{
try
{
write(fieldTF.getText());
}catch(Exception ex)
{
ex.printStackTrace();
}
}
public static void main(String args[]) throws SQLException , ClassNotFoundException
{
new FirstClass();
}
}
Here is the error:
java.lang.NullPointerException
at FirstYearProject.FirstClass.write(FirstClass.java:49)
at FirstYearProject.FirstClass.actionPerformed(FirstClass.java:58)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
I've already imported External Jars from DB2 to establish connection with Java. I think there's a problem with the query or problem in executing it.
There's a problem here:
PreparedStatement statement = null; // **** set to null here
String query = null;
query = "INSERT INTO NAMES VALUES (?)";
statement.setString(1,name); // **** use it here!
Where does statement become non-null before you use it?
You need to key in on line 49 and 58 of the FirstClass.java class as per the exception stacktrace message, FirstClass.java:49.
Also, you will want to learn the general concepts of how to debug a NPE (NullPointerException). You should critically read your exception's stacktrace to find the line of code at fault, the line that throws the exception, and then inspect that line carefully, find out which variable is null, and then trace back into your code to see why. You will run into these again and again, trust me.
So here:
java.lang.NullPointerException
at FirstYearProject.FirstClass.write(FirstClass.java:49)
at FirstYearProject.FirstClass.actionPerformed(FirstClass.java:58)
The stacktrace tells you to carefully inspect lines 49 and 58.
Note, you need to create a PreparedStatement object before you try to use it. First you'd create a Connection, and then you'd use that Connection to create the PreparedStatement via Connection's preparedStatement(...) method. This is all well described in the Java JDBC Tutorials. Pay particular attention to the PreparedStatement subsection.

Chat connection doesn't work

So I am trying to make a simple chat client but somehow the connection doesn't work. Can you help me? This is what i wrote:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;
public class game implements ActionListener{
JTextArea incoming;
JTextField outgoing;
BufferedReader reader;
PrintWriter writer;
Socket sock;
public static void main(String [] args){
game g = new game();
g.go();
}public void go(){
JFrame frame = new JFrame("Chat");
JButton sendB = new JButton("Send");
JPanel mainPanel = new JPanel();
incoming = new JTextArea(15,50);
incoming.setLineWrap(true);
incoming.setWrapStyleWord(true);
incoming.setEditable(false);
JScrollPane s = new JScrollPane(incoming);
s.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
s.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
outgoing = new JTextField(25);
sendB.addActionListener(this);
mainPanel.add(s);
mainPanel.add(outgoing);
mainPanel.add(sendB);
setUpnetworking();
Thread readerThread = new Thread( new IncomingReader());
readerThread.start();
frame.getContentPane().add(BorderLayout.CENTER, mainPanel);
frame.setVisible(true);
frame.setSize(800,500);
frame.setResizable(false);
}public void actionPerformed(ActionEvent e){
try{
writer.println(outgoing.getText());
writer.flush();
}catch(Exception ex){
ex.printStackTrace();
}
outgoing.setText("");
outgoing.requestFocus();
}public void setUpnetworking(){
try {
sock = new Socket("127.0.0.1", 5000 );
InputStreamReader streamreader = new InputStreamReader(sock.getInputStream());
reader = new BufferedReader(streamreader);
writer = new PrintWriter(sock.getOutputStream());
System.out.println("Connection Established");
} catch (IOException exx) {
exx.printStackTrace();
}
}public class IncomingReader implements Runnable{
public void run(){
String message;
try{
while ((message = reader.readLine()) != null){
System.out.println("read" + message);
incoming.append(message + "\n");
}
}catch (Exception exx){exx.printStackTrace();}
}
}
}
errors i got when i ran it:
java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at game.setUpnetworking(game.java:66)
at game.go(game.java:45)
at game.main(game.java:18)
java.lang.NullPointerException
at game$IncomingReader.run(game.java:79)
at java.lang.Thread.run(Unknown Source)
java.lang.NullPointerException
at game.actionPerformed(game.java:57)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
i really don't know what's the problem :( can someone help me so i can build a working Chat Client? I use Eclipse.
Sorry for bad Englisch, I am from The Netherlands
For a chat app you need both backend and client-side solutions.
To skip the part with server-side solution development you can use a ready backend and SDK provided by some platforms. That should save you a lot of time and effort and you will be able to concentrate on UI implementation.
Here are some providers you might consider using:
ConnectyCube
Firebase
Sendbird
Layer
Here is also an article comparing features provided by some of them.
You really need to build a server. You would need to go through networking a bit. But just in case you need a server, I made one few months back. Run it separately. See if you can make it work.
https://github.com/DhavalKapil/ForwardingServer

How to Play .wav File with JButton?

So recently, I have been trying to make my own Mario game (for myself, possibly to show my other friends). Games include buttons. When I click on a button in other games, it plays a sound. I would love to add that feature to my game. The problem is, it doesn't play. My source code is:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class JButtonClick {
JButton test = new JButton("Click Me!");
JPanel panel = new JPanel();
public void playSound(String soundName)
{
try
{
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File(soundName).getAbsoluteFile());
Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.start();
}
catch(Exception ex)
{
System.out.println("Error with playing sound.");
ex.printStackTrace( );
}
}
public JButtonClick() {
JFrame frame = new JFrame("Button-Click test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.pack();
frame.add(panel);
frame.setSize(800, 600);
frame.setResizable(true);
frame.setVisible(true);
panel.add(test);
test.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
playSound("JButton.wav");
}
});
}
public static void main(String[] args) {
new JButtonClick();
}
}
My .wav file is in the same package as this class. But instead of it playing the sound I want, it just says this:
java.io.FileNotFoundException: C:\Users\diego\workspace\Super Mario Bros 1\JButton.wav (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at com.sun.media.sound.WaveFloatFileReader.getAudioInputStream(Unknown Source)
at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
at JButtonClick.playSound(JButtonClick.java:21)
at JButtonClick$1.actionPerformed(JButtonClick.java:49)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
What am I doing wrong?!
Here is the problem due to placement of your .wav file.
As per your question your have placed it in the same package as this class that's why you are getting FileNotFoundException.
Let me explain you how new File(pathname) works:
Javadoc about File says:
A pathname, whether abstract or in string form, may be either absolute or relative. An absolute pathname is complete in that no other information is required in order to locate the file that it denotes. A relative pathname, in contrast, must be interpreted in terms of information taken from some other pathname. By default the classes in the java.io package always resolve relative pathnames against the current user directory.
It you are running this application in eclipse then place .wav file directly in the project (outside src folder) as shown below:
AudioSystem.getAudioInputStream(new File("Jbutton.wav"));
You can try also by placing it in resource folder.
AudioSystem.getAudioInputStream(new File("resources/Jbutton.wav"));
custom sounds play...using NetBeans..
First of all import some package or class name like this
import java.applet.Applet;
import java.applet.AudioClip;
import java.net.URL;
Not working because generate Error on java.lang.NullPointerException
URL url = getClass().getResource("\\dbmsystem\\src\\sound\\Your_sound_name.wav");
URL url = getClass().getResource("G:\\zala\\dbmsystem\\src\\sound\\Your_sound_name.wav");
But Proper Working like this
URL url = getClass().getResource("/sound/Your_sound_name.wav");
AudioClip clip = Applet.newAudioClip(url);
clip.play();

why i am getting Exception in thread "AWT-EventQueue-2" java.lang.NoClassDefFoundError?

i found a java file on the web that allows me to read a pdf in a url and save it to my local machine,
i have sucsesfully compiled it and customized as a javabean so i can useit in my app, but when i test it i am getting the next error message.
i have added the library (PDFOne.jar file) to my project in netbean and averything compiles well.
in fact, the program detect my pdf url and validate it saying is a valid pdf file, but then the error comes:
any tip ? i am completely new in java world.
thanks in advance
Exception in thread "AWT-EventQueue-2" java.lang.NoClassDefFoundError: com/gnostice/pdfone/PdfDocument
at Read_PDF_From_URL.setProperty(Read_PDF_From_URL.java:51)
at oracle.forms.handler.ComponentItem.setCustomProperty(Unknown Source)
at oracle.forms.handler.ComponentItem.onUpdate(Unknown Source)
at oracle.forms.handler.JavaContainer.onUpdate(Unknown Source)
at oracle.forms.handler.UICommon.onUpdate(Unknown Source)
at oracle.forms.engine.Runform.onUpdateHandler(Unknown Source)
at oracle.forms.engine.Runform.processMessage(Unknown Source)
at oracle.forms.engine.Runform.processSet(Unknown Source)
at oracle.forms.engine.Runform.onMessageReal(Unknown Source)
at oracle.forms.engine.Runform.onMessage(Unknown Source)
at oracle.forms.engine.Runform.processEventEnd(Unknown Source)
at oracle.ewt.lwAWT.LWComponent.redispatchEvent(Unknown Source)
at oracle.ewt.lwAWT.LWComponent.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: com.gnostice.pdfone.PdfDocument
at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 39 more
here is part of the source code:
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ConnectException;
import java.net.URL;
import java.net.URLConnection;
import oracle.forms.properties.*;
import oracle.forms.ui.*;
import com.gnostice.pdfone.PdfDocument;
import java.net.MalformedURLException;
public class Read_PDF_From_URL extends VTextArea {
private static final ID GEN = ID.registerProperty("sav");
public boolean setProperty(ID id, Object value) {
boolean retorno = true;
try {
if (id == GEN) {
System.out.println("if");
URL url1 =
new URL("http://www.gnostice.com/downloads/Gnostice_PathQuest.pdf");
byte[] ba1 = new byte[1024];
int baLength;
FileOutputStream fos1 = new FileOutputStream("sibdownload.pdf");
// Contacting the URL
System.out.print("Connecting to " + url1.toString() + " ... ");
URLConnection urlConn = url1.openConnection();
// Checking whether the URL contains a PDF
if (!urlConn.getContentType().equalsIgnoreCase("application/pdf")) {
System.out.println("FAILED.\n[Sorry. This is not a PDF.]");
} else {
try {
// Read the PDF from the URL and save to a local file
InputStream is1 = url1.openStream();
while ((baLength = is1.read(ba1)) != -1) {
fos1.write(ba1, 0, baLength);
}
fos1.flush();
fos1.close();
is1.close();
// Load the PDF document and display its page count
System.out.print("DONE.\nProcessing the PDF ... ");
PdfDocument doc = new PdfDocument();
try {
doc.load("sibdownload.pdf");
System.out.println("DONE.\nNumber of pages in the PDF is " +
I have resolved my question... The first thing was that I've needed to include the jar file in my classpath and the second thing that I've needed to sign the jar file.

Categories