Ronald Vermeij

Ranch Hand
+ Follow
since Sep 05, 2009
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
2
Received in last 30 days
0
Total given
2
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Ronald Vermeij

Paul Sturrock wrote:Hard to say. Can you change you code to log the stack trace and post it?


Thanks for answering Paul.. I already found the answer by digging deeper into the error myself. Here is what happend:

Location of the MySql-JDBC driver:
A - current directory (just to be sure)
B - /usr/share/java/mysql-connector-java-5.1.13-bin.jar

1. When I first started the program like this
java MySqlLoadDriver<enter>
I got this error -> Exception: com.mysql.jdbc.Driver

2. When i added the -cp (classpath) to startup parameters
java -cp /usr/share/java/mysql-connector-java-5.1.13-bin.jar MySqlLoadDriver
I got this error:
Exception in thread "main" java.lang.NoClassDefFoundError: MySqlLoadDriver
Caused by: java.lang.ClassNotFoundException: MySqlLoadDriver
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:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: MySqlLoadDriver. Program will exit.

3. Then i added the CURRENT DIRECTORY to the -cp (classpath) like this:
java -cp .:/usr/share/java/mysql-connector-java-5.1.13-bin.jar MySqlLoadDriver
MySQL JDBC driver loaded ok.

What was the problem? (an the cause of the error)
1. The JVM did find its way to my own .class file..... (and could not find the MySql jdbc driver)
2. The JVM did find the MySql jdbc driver .............. (and could not load my own .class file)
3. The JVM found both - via the right (and complete) classpath

Thanks fro the motivation Paul!
1. I've installed MySql on my Linux box + tools (MySql Admin tool, Workbench, Query browser)
2. Created the following MySql database account:
Mysql user............: root
Mysql password.....: root
Mysql host.............: localhost
Mysql port.............: 3306 (default)
This user has been granted right to Schema/ table called "test"

2. I've installed the official MySql JDBC driver in.. /usr/share/data
3. I've set the %CLASSPATH% to....................... /usr/share/java

4. Complided the following code without errors


5. When running this code via java JdbcExample2<enter>
I get the following output: Exception: com.mysql.jdbc.Driver

6. It looks like the program can not find the JDBC driver.. am i correct?

Question:
- What am I missing in this jdbc_example?
- How can I find out WHAT is the cause of the exception
- Which java code do i need to add to gain more insight in what is wrong
(is it a java error, mysql server configuration / user_permissions error)

Thanks for helping me with this "vague" exception:

p.s.
This error keeps occuring..:
- without the MySql database daemon running
- with the MySql database daemon running
- without a valid userid & password
- with a valid userid & password
I guess it is back to "Ground Zero" /"Square One" regarding to Swing Applet building:-)
Thanks Everyone!
8 years ago
I am learning "Swing by example" by translating an good working simple AWT applet into a Swing Applet. To keep it very simple (and understand the basic first) I only add a button to an applet_surface and a button_actionlistener to that button.

Now i'm running to a brick wall... seems like I can not find a solution why my Swing applet is not working :-(

AWT Applet code


Swing applet code


Both examples compiles without errors, but when loading the applets into a browser via an html page...:
- The AWT applet shows the [quit] button on the applet_surface
- The Swing version shows nothing :-(

Question
What have I forgotten in the Swing version?
Do I need use a JFrame first to which I add the JButton before it gets visible on screen?
8 years ago

Stephan van Hulst wrote:Good luck!


Thanks Stephan
8 years ago

Stephan van Hulst wrote:Yes, you can use a (J)Window for those purposes.

I would highly recommend going straight to Swing, and checking out the tutorial Rob gave you. Swing is very much alike AWT, so after you've learned Swing, you'll understand most of AWT as well.

There's a big difference in the way components are drawn though, for example. Swing makes all of this relatively easy, with its heavy use of so called 'lightweight' components.

Most of Swing's event handling comes straight out of AWT.

After you're familiar with working with Swing, and making decent GUI applications with it, would I go back and find out how AWT works exactly, and how Swing builds on top of it. It's easier to understand if you have already worked with making GUIs.



Thanks Stephan:
I've already started sniffing around in the Swing Tutorial that Rob gave me yesterday. Another reason why I started with AWT-GUI-Building is that I am learning Java from my first original book i've bought some 13 years ago - "Sunsoft Java WorkShop in 21 days" (and never finished it back then :-( ) and a recently purchased 2nd hand copy of "The Complete Java training Course" by Deitel and Deitel. Those beautiful books are filled with AWT only knowledge.

Beside that, I have surfed and found a lot of Swing tutorials only, which I now will use to expand my java-gui-builders knowledge.
8 years ago

Ernest Friedman-Hill wrote:1) It's generally a javax.swing.JApplet or a java.applet.Applet; either way, it's ultimately a subclass of java.awt.Panel.
2) You can set the size in the HTML; you can't change it afterwards.



Thanks Ernest for clearing this up / confirming my test results!
I have experimented with this "strange applet behavior" and created some 20 (AWT and Swing) little ("hello world") applets (with window, frame or panel as "display-surface"basis) + 20 HTML webpages (with and without the "height" and "width" parameters), just to see how the stuff works, by trail and error :-(.

No Java book (i've ready for far) and online Java applet tutorial mentions this strange feature!

So far I've found of that a (J)Frame has properties to:
- .setSize(w,h) its "display_surface" area to a pre-determined height and width
- .setResizable(true.false) to (dis)allow resizing the "display_surface" area

Note:
Funny thing don't you think that Java has no option to pre-fix a application/applet "display area size"??
Or is this "left out" on purpose because Java is designed as a Multi platform and Multi Operating System Programming Language.???

8 years ago

Rob Camick wrote:Rarely would you use any of those components. Most people these days would use Swing components (JFrame, JWindow, JPanel).
General rule is an application would have a single JFrame. If you need child windows to gather information or display properties of your application then you use modal JDialogs. A JFrame appears on the task bar, a JDialog doesn't.

Hello Rob,
I've started with the AWT toolkit, just to get my Java-GUI Basics covered and trying to understand how everything interlock with eachother like buttons+evenlisteners, choice+ItemstateChangers etc...


Rarely would you use a JWindow. One case would be when you don't want to display borders and you don't need any input from the user (like a splash screen), just a "Close" button.

So when I want a fullscreen, borderless gui, i choose a (J)window?


I suggest you take a look at the Swing tutorial for the basics.

Thanks for this link Rob...

8 years ago
When creating an "gui" on top of an applet-surface.. most examples i have found - simply start put gui element (on what?) en voila...
Their you have it.. an applet-gui-surface with couple of gui-elements

Question 1: What IS the applet surface actually?
Is it
- a Window?
- a Frame?
- a Panel?
- something else?

Question 2: How can you set (and fix) it height and width? or is this simply not possible?

Thanks for de-mistifying this Applet-Gui-Thing

8 years ago
Hallo All,
I have a hard time figuring out when I use what GUI-element as a BASIS for a GUI.
java.lang.Object
-java.awt.Component
--java.awt.Container
---java.awt.Window
----java.awt.Frame AND ----java.awt.Panel

- the "Panel thing" I have figured out:
This is can be used to arrange buttons, choices, list, textboxes in a pre-determined order and shape
Entire panels then, can be combined into a single GUI screen with the help of a layout manager like (flow, border, grid, gridbag...)

Question: Can you give me some "rules of the tumb" regarding what GUI element to choose in which situations?
- When do you choose a WINDOW as basis of a GUI?
- When do you choose a FRAME as basis of a GUI?

thanks for sharing your GUI wisdom with me
8 years ago
Hello All,
I am working my way through GUI programming and find myself frustrated about not being able to fix a screens dimensions (yet)

So far I have figured out that:
1. "The screensize" of an applet can be regulated from the HTML code that launches the applet. The parameter width and height determine how big the "applet screen area" is going to be "on screen in the browser" of the client. (see example below)


Question:
How can I determine/fix the surface_area of an applet (from inside the applet via code)
8 years ago
Hello Java Guru's
Scenario: The Classic Producer - Cubbyhole - Consumer example from Sun Microsystems



My challenge
I'm writing a "third thread" called "monitor_status".
Via this 3rd thread I want to
- get information out of the (java object) monitor that guards the Cubyhole object.
- to monitor the Thread.status of both the Producer and the Consumer threads.

Via the Thread. getState() Is can see in which status a thread is [new],[runnable][,...], etc...
I have not discovered any "java.lang.Object" Class method that provides access to monitor/lock information.


Question:
1. Is it possible (with the example above) to get information on the status of the monitor / lock on the Cubyhole object?
2. Is it possible to look into the monitors ENTER_queue and WAIT_queue? (to see which calling thread are in which monitor_queue)
or...
3. Do i have to rewrite this example to make use of the "java.util.concurrent.locks" Lock object instead of wait(), notify() methods?


I do have found the java.lang.management.MonitorInfo Class, but i can not find a good example that shows me how to use it.
4. Do you have an code-example on how to use this class?

Thanks for sharing your insight on this java monitor / object lock matter.



Ronald : Can you explain what is happening in the code below between the producer and consumer threads? The code uses the wait() and notify() tools of the java API with 'queue' as the lock object.


- 1 Queue object is created (in the form of a LinkedList object)
- 3 consumerthreads are created, initialized and started
- 3 producerthreads are created, initialized and started
- All 6 threads "are connected" / "communicate" via 1 and the same "queue" object.

Producer thread steps (in plain English)
- has a "self-ending" threadloop after 10 cycles (for..loop)
- generates a random number
- locks the queue object (to get single acces to it), via the synchronized mechanism
so one 1 (producer or consumer) thread at the time can have access to the queue object.
- "offers" the random number (num) to be placed in the queue object via queue.offer method.
- The value inside (num) is being placed into the LinkedList.
- is ready with its operation on the queue object,
- releases the "object lock" from the queue object,
- notifies this good news (queue object is free again :-) ! )
to the other threads who want to have access to the queue object
- prints out the random number
- goes to sleep for a while.
and repeat these steps above 10 times.


Consumer run() thread steps (in plain English)
- has an "ever lasting" threadloop .. while (true)

- locks the queue object for single access via the synchronized method.
- "looks in the queue", if their is any data present via queue.peek method
- If queue is empty,
... the consumer thread is put in "wait mode".. until the producerthread "notifies" the consumerthread
... that it has put some data into the queue. In 'wait mode" queue objectlock is released , so other threads
... can try to lock the queue object for themselves.

- When consumer thread has found data in the queue,
... it retrieves the first data element from the queue via "queue.pol" method

- This data from the queue is then printed on the console
- These steps are repeated for every (or until press Ctrl-C on the Linux console commandline)

the funny thing on this example is.. that it differs from the average Jim.
Since must P_C examples i've seen so far the producerthread "waits" and the consumerthread "notifies"
But anyway.. it does not matter, as long as it works correctly according to Java API rules

Garik Ustinov wrote:You have 4 Threads working together without a single synchronized block, concurrency api usage or even volatile variable. I guess it just cannot be right. Take a closer look, maybe there's something wrong with it.


Thanks Garik for pointing this out. This is how it should be done in relation to synchronized block(see code fragments)
Listing 2 and 3
Listing 3

Garik Ustinov wrote:There are plenty of Producer-consumer examples java producer consumer available.
I suggest you to start with some simpler examples, rather than digging in Java Concurrency in Practice.



Thanks to your hint..I have found some very beautiful, visually supportive examples online.
I can reccomend them to anyone who is learning Java Multi-threading!

George Mason University - department of computer science
1. Producer_Consumer - Semaphore based:
http://www.cs.gmu.edu/cne/workbenches/pcsem/pcsem.html
http://www.cs.gmu.edu/cne/workbenches/pcsem/Semaphore.html

2. Producer_Consumer - Monitor based:
http://www.cs.gmu.edu/cne/workbenches/pcmon/pcmon.html
http://www.cs.gmu.edu/cne/workbenches/pcmon/monitor.html

3. Producer_Consumer - Message based:
http://www.cs.gmu.edu/cne/workbenches/pcmsg/pcmsg.html
http://www.cs.gmu.edu/cne/workbenches/pcmsg/Message.html

Website of Rafael Stekolshchik:
http://cities.lk.net/approco.html


Thanks for your feedback Garik
Hey Jim,
Thank you very much for taking the time and effort to study my listings!

To answer your question:
1. Yes I am trying to understand how I can create multi-threaded code.
The purpose of listing 1 and 2 is to investigate how I can created "single stand-alone" threads,
- that can be "launched" by a "mother program" (the launcher)
- which will run "for ever", once started
- which can be "remotely controlled" (started, pauzed and terminated) from outside the thread itself.

The purpose of listing 2 and 3 is to investigate how I can created
- "multiple producers" vrs a "single consumer" thread
- inter-thread communication mechanisms like..
-> How to check if the (other) thread alive?
-> How to check if the (other) thread is accessible for calling threads?
-> How the (other) thread can tell his environment that he is busy / free?
-> When to use an (every lasting) THREAD as consumer? vrs
-> When to use a (dead) OBJECT as consumer?


2. I also want to get "a good feeling. insight" with multi-threading programming on how and when to use the
various multithreaded communication and synchronization mechanisms like:
- (wait, notify) -------------------------------------------------------> producer, consumer scenario
- (wait, notify_all) ----------------------------------------------------> producer, consumer
- calling synchronized {blocks of code}----------------------------> multiple threads with 1 (or more) shared object
- calling {entire methods} from one thread to another thread -->multiple threads with 1 (or more) shared object
in what kind of different scenarios.

I have read (and played with) lots of code examples from books like:
- Concurrency examples" - Sun Microsystem own tutorial
- "Concurrency: State Models & Java Programs" - J Kramer, J Magee (the best book for me so far!!)
- "Java threads 2nd edition" - O Reilly
- "Multi-threaded programming with java technology" - Sun Press
- "Java thread programming" - Sams
- "Java Concurrency in Practice" - Brian Goetz
- "Inside the Java Virtual machine" - Bill Venners
- "Thinking in Java" - Bruce Eckel
And numerous online tutorials, downloadable code examples and education materials i could find like
Jakob Jenkov - Java Concurrency tutorials - http://tutorials.jenkov.com/java-concurrency/index.html

As you can see i have used all kind of different sources on Java multi-threading technologies, to from a clear picture on when to use what technology in which situation...

The reason I asked my question is that i have a "gut feeling" that i can solve this current challenge ALSO with Wait, Notify (since it is a producer, consumer scenario,pattern) right... but i have not exactly figured out how.... and that is why i need a little help in modifing listing 2 (for the wait) and 3 (fro the notify, notify all) statement to be injected