Archive | Java RSS feed for this section

Error compiling on CentOS

24 Apr

I was receiving:
error: IO exception sun.io.MalformedInputException
1 error

Turns out this is because of the language:
export LANG=""

fixed it.

Advertisements

VSFTPD over SSL

6 Sep

Need to add the following to bottom of /etc/vsftpd/vsftpd.conf:

ssl_enable=YES
ssl_tlsv1=YES
ssl_sslv2=YES
ssl_sslv3=YES
rsa_cert_file=/etc/vsftpd/vsftpd.pem
force_local_data_ssl=YES

Then you have to create the /etc/vsftpd/vsftpd.pem file:

openssl req -x509 -nodes -days 7300 -newkey rsa:2048 -keyout vsftpd.pem -out vsftpd.pem

To export this .pem file so that a client can import it, use:

openssl x509 -outform DER -in vsftpd.pem -out cacert.cert

Then a java client can import the certificate doing:

keytool -import -trustcacerts -alias mycert -file cacert.cert -keystore mykeystore

JSchException – invalid privatekey – ant – scp

2 Feb

Trying to use scp target in ant with a ‘keyfile’, i had previously used puttygen to make myself a key (and it worked fine through putty).

With Ant i was getting:

$> build.xml:21: com.jcraft.jsch.JSchException: invalid privatekey: C:\id_rsa.ppk

Solution:
Load up puttygen and “Export” the key in ‘openssh’ format. Try using this key instead.

Cast Superclass to Subclass

21 Dec

http://forum.java.sun.com/thread.jspa?threadID=550048&messageID=2685097

Java Swing GUI Threads/FoxTrot/SwingWorker/SwingUtilities

16 Oct

Java GUI’s:
(Summarising Ben Galbraith excellent tutorials on javalobby)

Native OS -> AWT Event Queue <- AWT Event Dispatch Thread

All GUI operations have to occur on the Event Dispatch Thread (EDT).
It happens that all EventListeners are also processed on EDT.

This means that if an EventListener has heavy code in it, the EDT slows down
and GUI operations (e.g. redraw screen, processes mouse click) slow down as
they remain in the AWT Event Queue (the EDT is busy processing your heavy code).
This will result in a 'laggy' interface for the user.

(
Generally, all operations modifiying REALIZED Swing components must take place
on the EDT, but there are some exceptions. These methods are Thread-safe:

JComponent.revalidate()
JComponent.repaint()
JComponent.invalidate()
JTextComponent.setText(String)
JTextComponent.replaceSelection(String)

)

Need Threads for a responsive GUI. A few ways to implement this.

0. Non threaded, laggy original:

dirButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String directory = dirText.getText();
final File dirFile = new File(directory);
if (!dirFile.exists()) return;

DefaultListModel dirListModel = new DefaultListModel();
addFilesToModel(dirFile, dirListModel); // THIS IS THE LAGGY LINE
dirList.setModel(dirListModel); // THIS LINE UPDATES THE GUI
}
});

1. Use SwingUtilities for an asynchronous Thread
If you have an EventListener that you want to update the GUI, you have to
have the EDT do the GUI update (the EDT has to do ALL GUI updates!!!).

dirButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String directory = dirText.getText();
final File dirFile = new File(directory);
if (!dirFile.exists()) return;

dirButton.setEnabled(false); // DISABLE TO PREVENT USER CLICKING IT AGAIN!

new Thread() {
public void run() {
final DefaultListModel dirListModel = new DefaultListModel(); // HAS TO BE FINAL AS PASSED TO INNER CLASS
addFilesToModel(dirFile, dirListModel); // THIS IS THE LAGGY LINE
SwingUtilities.invokeLater(new Runnable() {
public void run() {
dirList.setModel(dirListModel); // THIS LINE UPDATES THE GUI ON THE EDT
dirButton.setEnabled(true); // RE-ENABLE BUTTON
}
});
}
}.start();
}
});

2. Use SwingWorker for an asynchronous Thread (not part of JDK)
SwingWorker basically tidys up SwingUtilities, also with a good IDE you can just
refactor this out. Converting this anonymous inner class into an inner class which
can be moved around easily.


dirButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String directory = dirText.getText();
final File dirFile = new File(directory);
if (!dirFile.exists()) return;

dirButton.setEnabled(false); // DISABLE TO PREVENT USER CLICKING IT AGAIN!
SwingWorker swingWorker = new SwingWorker() {
public Object construct() {
// NOTE !!! - THIS DOES NOT HAPPEN ON THE EDT, SO NO GUI OPERATIONS HERE
DefaultListModel dirListModel = new DefaultListModel(); // NO LONGER HAS TO BE FINAL
addFilesToModel(dirFile, dirListModel); // THIS IS THE LAGGY LINE
return dirListModel; // THIS CAN RETURN ANY OBJECT
}

public void finished() {
// NOTE !!! - THIS *DOES* HAPPEN ON THE EDT, SO ALL GUI OPERATIONS HERE -NO LAGGY THINGS
ListModel dirListModel = (ListModel) get(); // GET THE dirListModel RETURNED BY THE 'construct()' METHOD
dirList.setModel(dirListModel); // THIS LINE UPDATES THE GUI ON THE EDT
dirButton.setEnabled(true); // RE-ENABLE BUTTON
}
};
swingWorker.start();
}
});

3. Use FoxTrot for a synchronous Thread
GOOD TO RETROFIT APPLICATIONS WHEN YOU DON’T HAVE PROPER THREADS.

FoxTrot is synchronous, it has access to the Event Queue and can process events in it.
In the following code it looks like the “Worker.post()” call should block and hang waiting
for the “addFilesToModel()” method to return. But in reality FoxTrot is spawning a background
thread to process the commands in its “run()” method. It then monitors the background
thread and waits for it to return, in the meantime it processes anything in the Event
Queue, keeping the GUI responsive. (It can do this because the EDT is the thread that
executes the Listener itself AND FoxTrot’s “post()” method, hence it can access the Event Queue).


dirButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String directory = dirText.getText();
final File dirFile = new File(directory);
if (!dirFile.exists()) return;

dirButton.setEnabled(false); // DISABLE TO PREVENT USER CLICKING IT AGAIN!

ListModel dirListModel = (ListModel) Worker.post(new Job() {
public Object run() {
DefaultListModel dirListModel = new DefaultListModel();
addFilesToModel(dirFile, dirListModel); // THIS IS THE LAGGY LINE
return dirListModel; // THIS CAN RETURN ANY OBJECT
}
});

dirList.setModel(dirListModel); // THIS LINE UPDATES THE GUI ON THE EDT
}
});

Abbot – Java GUI Testing

16 Oct

I was having trouble with launching a program that used the newer (2.7) Xerces library as Abbot itself was using xerces (in lib/).

I tried using “-Djava.endorsed.dirs” on when starting Abbot(i.e. ‘java -Djava.endorsed.dirs=C:\xerces -jar lib/Abbot.jar’) to no avail.

I also tried adding it as a vmarg in ‘Test->Additional VM Args’. I also tried putting my xerces jar at the top of the classpath.

In the end I just replaced the lib/xerces.jar in the Abbot directory with my 2.7.0 xercesImpl.jar (I renamed it), and things work.

The error I was receiving:

061016 15:43:34:483 abbot.script.EventExceptionHandler.exceptionCaught(EventExceptionHandler.java:22):
Unexpected exception while dispatching events:
061016 15:43:34:483 Error at org.jboss.ws.tools.JavaToXSD.parseSchema(JavaToXSD.java:189)
at org.jboss.ws.metadata.wsdl.WSDL11Reader.processTypes(WSDL11Reader.java:227)
at org.jboss.ws.metadata.wsdl.WSDL11Reader.processDefinition(WSDL11Reader.java:118)
...
...
at java.awt.EventDispatchThread.run(Unknown Source): java.lang.NoSuchMethodError: org.apache.xerces.impl.xs.XMLSchemaLoader.loadURIList(Lorg/apache/xerces/xs/StringList;)Lorg/apache/xerces/xs/XSModel;

Java 5 Constants

13 Oct

Java 5 has better support for constants, implemented as an interface

// Create like:
public interface Constants {
public static final String WELCOME = “welcome”;
}


// Use like:
import static Constants.*;
public class test{
public static void main(String[] args) {
System.out.println(WELCOME);
}
}