I've started getting a serious problem by the beggining of this week when our customer got new Smartcards version to use on one of our products, during the keyStore loading it throws an exception like this:
java.lang.Exception: Login failure: java.io.IOException: ObjectIdentifier() -- data isn't an object ID (tag = -95)
at certificate.helper.HelperClass.validateCard(HelperClass.java:194)
at certificate.helper.HelperClass.retrieveToken(HelperClass.java:107)
at certificate.view.LoginDialog.validate(LoginDialog.java:144)
at certificate.view.LoginDialog$1.actionPerformed(LoginDialog.java:84)
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$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)
To get things worse, I just can't simulate it on development env!
I started thinking it is something related to user's environment, here our team use Windows 10 x64 + JDK 1.8_x 64, we can simulate this same error in some statios with Windows 7 x64 + JRE 1.8_x x86, our customer got also Linux in some station and they said it works fine.
I tried to change keyStore model and the way it is loaded but it seems nothing solves the problem! Here is how things were done:
public TokenVO retrieveToken(String pin) throws Exception {
Provider pkcs11Provider = new SunPKCS11(leitorGemPC_Windows())
Security.addProvider(pkcs11Provider);
Security.addProvider(new BouncyCastleProvider());
KeyStore smartCardKeyStore = createKeyStore("PKCS11");
validateCard(pin, smartCardKeyStore);
Enumeration aliasesEnum = smartCardKeyStore.aliases();
while (aliasesEnum.hasMoreElements()) {
.... things don't even get this far
}
public void validateCard(String pin, KeyStore smartCardKeyStore) throws Exception {
try {
smartCardKeyStore.load(null, pin.toCharArray());
}
catch (Exception e) {
e.printStackTrace();
throw new Exception(e.getMessage());
}
}
public KeyStore createKeyStore(String keyStoreName) throws KeyStoreException {
if (keyStoreName== null || keyStoreName.isEmpty()) {
keyStoreName= DEFAULT_KEYSTORE;
}
KeyStore smartCardKeyStore = KeyStore.getInstance(keyStoreName);
return smartCardKeyStore;
}
The only keyStore that really works is the PKCS11, every other I tried don't validate user's Smartcard PIN/Password allowing to use anything even a blank password and return an empty aliasesEnum so that data cannot be retrived from the card.
Is there something I can do?
Related
First time posting but here it goes. I am attempting to to make a program that reads all files in a drive and hash those files in order to spot potential differences in a "corrupt drive". The program functions fine for all purposes except when encountering files with special permission needs. I have spent days trying to resolve this and have read many stack posts but none that i have found have covered my specific issue. I have tried granting permission through set read and write but does nothing.
I think i found a promising library through java.security.allpermissions but finding examples of how to set it up simply / clearly for a method call has been unfruitful.
The following is the method giving me a hard time.
private static String hash(File f, String name) throws NoSuchAlgorithmException, IOException {
if(f.isDirectory() == true) {
return "";
}
InputStream is = Files.newInputStream(Paths.get(f.getAbsolutePath()));
output = DigestUtils.sha256Hex(is);
output = DigestUtils.sha256Hex(name + output);
return output;
}
sample output + stack trace:
F:\
F:\$Recycle.Bin
F:\$Recycle.Bin\S-1-5-18
F:\$Recycle.Bin\S-1-5-18\desktop.ini
F:\$Recycle.Bin\S-1-5-21-3562049501-1359381880-3951677873-1001
F:\$Recycle.Bin\S-1-5-21-3562049501-1359381880-3951677873-1001\desktop.ini
F:\$Recycle.Bin\S-1-5-21-4029137503-3596887599-1249178651-1001
F:\$Recycle.Bin\S-1-5-21-4029137503-3596887599-1249178651-1001\desktop.ini
F:\Documents and Settings
Issue exploring folder:
java.nio.file.AccessDeniedException: F:\hiberfil.sys
at sun.nio.fs.WindowsException.translateToIOException(Unknown Source)
at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
at sun.nio.fs.WindowsFileSystemProvider.newByteChannel(Unknown Source)
at java.nio.file.Files.newByteChannel(Unknown Source)
at java.nio.file.Files.newByteChannel(Unknown Source)
at java.nio.file.spi.FileSystemProvider.newInputStream(Unknown Source)
at java.nio.file.Files.newInputStream(Unknown Source)
at Core.Main.hash(Main.java:47)
at Core.Main.construct(Main.java:91)
at Core.Main.run_construct(Main.java:129)
at Core.Display$SwingAction.actionPerformed(Display.java:297)
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.AWTEventMulticaster.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$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)
With Java and BouncyCastle I am able to decrypt lots of S/MIME enveloped messages.
But when the key encryption algorithm is 1.2.840.113549.1.1.7 (id-RSAES-OAEP), the decryption fails (stack trace at the end of this question).
The line where it happens:
content = messageRecipientInfo.getContent(new JceKeyTransEnvelopedRecipient(pc).setProvider("BC"));
Formerly I used that line to decrypt:
content = messageRecipientInfo.getContent(givenCertRecipient);
It's all the same.
Any guess?
EDIT *
I now tried to ENCRYPT data with OAEP. Therefore I've found code directly inside BouncyCastle in the file cms/test/NewEnvelopedDataTest.java
Even that doesn't work!
The same error:
String BC = BouncyCastleProvider.PROVIDER_NAME;
KeyPair _reciKP = CMSTestUtil.makeKeyPair();
String _reciDN = "CN=Doug, OU=Sales, O=Bouncy Castle, C=AU";
KeyPair _signKP = CMSTestUtil.makeKeyPair();
String _signDN = "O=Bouncy Castle, C=AU";
X509Certificate _reciCertOaep = CMSTestUtil.makeOaepCertificate(_reciKP, _reciDN, _signKP, _signDN);
byte[] data = "WallaWallaWashington".getBytes();
CMSEnvelopedDataGenerator edGen = new CMSEnvelopedDataGenerator();
JcaAlgorithmParametersConverter paramsConverter = new JcaAlgorithmParametersConverter();
edGen.addRecipientInfoGenerator(new JceKeyTransRecipientInfoGenerator(_reciCertOaep).setProvider(BC));
edGen.addRecipientInfoGenerator(new JceKeyTransRecipientInfoGenerator(ASN1OctetString.getInstance(ASN1OctetString.getInstance(_reciCertOaep.getExtensionValue(Extension.subjectKeyIdentifier.getId())).getOctets()).getOctets(), _reciCertOaep.getPublicKey()).setProvider(BC));
Here during the last step it crashes: "cannot initialise algorithm parameters: Operation not supported"
CMSEnvelopedData edd = edGen.generate(
new CMSProcessableByteArray(data),
new JceCMSContentEncryptorBuilder(CMSAlgorithm.DES_EDE3_CBC).setProvider(BC).build());
What is the problem? I can't see it...
org.bouncycastle.cms.CMSException: exception unwrapping key: cannot
initialise algorithm parameters: Operation not supported at
org.bouncycastle.cms.jcajce.JceKeyTransRecipient.extractSecretKey(JceKeyTransRecipient.java:169)
at
org.bouncycastle.cms.jcajce.JceKeyTransEnvelopedRecipient.getRecipientOperator(JceKeyTransEnvelopedRecipient.java:26)
at
org.bouncycastle.cms.KeyTransRecipientInformation.getRecipientOperator(KeyTransRecipientInformation.java:48)
at
org.bouncycastle.cms.RecipientInformation.getContentStream(RecipientInformation.java:169)
at
org.bouncycastle.cms.RecipientInformation.getContent(RecipientInformation.java:150)
at CryptoTools.decryptAndVerifyFile(CryptoTools.java:1030) at
FormMain$2.actionPerformed(FormMain.java:403) 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$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:
org.bouncycastle.operator.OperatorCreationException: cannot initialise
algorithm parameters: Operation not supported at
org.bouncycastle.operator.jcajce.OperatorHelper.createAlgorithmParameters(OperatorHelper.java:254)
at
org.bouncycastle.operator.jcajce.JceAsymmetricKeyUnwrapper.generateUnwrappedKey(JceAsymmetricKeyUnwrapper.java:100)
at
org.bouncycastle.cms.jcajce.JceKeyTransRecipient.extractSecretKey(JceKeyTransRecipient.java:158)
... 42 more Caused by: java.io.IOException: Operation not supported
at
org.bouncycastle.jcajce.provider.asymmetric.rsa.AlgorithmParametersSpi$OAEP.engineInit(AlgorithmParametersSpi.java:83)
at java.security.AlgorithmParameters.init(Unknown Source) at
org.bouncycastle.operator.jcajce.OperatorHelper.createAlgorithmParameters(OperatorHelper.java:250)
... 44 more
I am attempting to have my Java Application open a PDF file when the user clicks button. However, I get the stack trace below stating that the file doesn't exist. Bascially I would like to be able to load this file when the user makes the selection.
Below I will have the stack trace then the code and a screenshot of the path.
StackTrace:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: The file: \RFBase-TD_Communications\src\pdf\RFTDAnalyzerHelpFile.pdf doesn't exist.
at java.awt.Desktop.checkFileValidation(Unknown Source)
at java.awt.Desktop.open(Unknown Source)
at GUI.rfbgui.openPDF(rfbgui.java:787)
at GUI.rfbgui.access$7(rfbgui.java:773)
at GUI.rfbgui$6.actionPerformed(rfbgui.java:921)
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.AbstractButton.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.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)
Code:
private static void openPDF()
{
File pdfHelpFile = new File("/RFBase-TD_Communications/src/pdf/RFTDAnalyzerHelpFile.pdf");
try
{
Desktop.getDesktop().open(pdfHelpFile);
}catch(IOException ex)
{
ex.printStackTrace();
}
}
I have some general advice for how to handle these situations. Files were one of the things I got very frustrated with when starting to learn to program.
Use System.getProperty("user.dir"); This can be very helpful especially when you do not know where the program is going to be run from, or you have a specific file structure.
In Java, I generally recommend using "\" instead of "/".
Run a sanity check on the file you are attempting to load. Specifically check if it is null, .isFile(), etc. You never know what you might get back, so its good to take a peak before accidently crashing your program.
Here is some links for similar questions that might help you out;
How should I load files into my Java application?
Getting the Current Working Directory in Java
Getting the inputstream from a classpath resource (XML file)
File myFile = new File(getClass().getResource("/files/test.pdf").toURI());
or
if (Desktop.isDesktopSupported()) {
try {
File myFile = new File("/path/to/file.pdf");
Desktop.getDesktop().open(myFile);
} catch (IOException ex) {
// no application registered for PDFs
}
I have problem with my applet.It works fine in applet viewer but in the Web browser it throws an Exception.
Exception details:
`SQLException: No suitable driver found for jdbc:mysql://s46.eatj.com:3307/cmentarz
Exception in thread "AWT-EventQueue-2" java.lang.NullPointerException
at JDBC.LogIn(JDBC.java:49)
at AdminPanelLogIn$1.actionPerformed(AdminPanelLogIn.java:64)
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.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)`
I already have signed my jar file using JARmaker.
My getConnection method code:
public Connection getConnection() throws SQLException {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}
try {
con = DriverManager.getConnection(url, userid, password);
} catch (SQLException ex) {
System.err.println("SQLException: " + ex.getMessage());
}
return con;
}
Userid,password and url are correct because it works in applet viewer.
static String url = "jdbc:mysql://s46.eatj.com:3307/cmentarz";
So, what could be the problem?
Make sure that the MySQL jar is available to your applet when running in a browser. You can specify this in your applet (or object) tag:
<applet code="org.package.MyApplet" width="800" height="600" archive="myappletjar.jar,mysql-connector-java-5.1.6-bin.jar">
</applet>
Also make sure that the jar file exists in the same location as your applet jar file so that it is available for download.
When using a Java robot, when sending a slash or backslash throws an exception.
For example:
public void slash() throws AWTException {
Robot rob = new Robot();
rob.keyPress(KeyEvent.VK_SLASH);
rob.keyRelease(KeyEvent.VK_SLASH);
}
public void backSlash() throws AWTException {
Robot rob = new Robot();
rob.keyPress(KeyEvent.VK_BACK_SLASH);
rob.keyRelease(KeyEvent.VK_BACK_SLASH);
}
Then, when I want to type those, I use:
public void type() {
try {
slash();
} catch (AWTException e) { System.out.println("Exception when typing slash."); }
try {
backSlash();
} catch (AWTException e) { System.out.println("Exception when typing back slash."); }
}
I get two error messages in my console. By the way, all other keystrokes I tried to send worked fine.
I get the follwowing stacktrace for slash:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Invalid key code
at sun.awt.windows.WRobotPeer.keyPress(Native Method)
at java.awt.Robot.keyPress(Unknown Source)
at com.paschoalinoto.bruno.pastescript.Paste.slash(Paste.java:23)
at com.paschoalinoto.bruno.pastescript.Paste.type(Paste.java:36)
at com.paschoalinoto.bruno.pastescript.MainGUI$4.actionPerformed(MainGUI.java:113)
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)
Backslashes work, but also throw an IllegalArgumentException:
java.lang.IllegalArgumentException: Invalid key code
at sun.awt.windows.WRobotPeer.keyPress(Native Method)
at java.awt.Robot.keyPress(Unknown Source)
at com.paschoalinoto.bruno.pastescript.Paste.press(Paste.java:198)
at com.paschoalinoto.bruno.pastescript.Paste.paste(Paste.java:173)
at com.paschoalinoto.bruno.pastescript.Paste.finalPaste(Paste.java:227)
at com.paschoalinoto.bruno.pastescript.MainGUI$4.actionPerformed(MainGUI.java:113)
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)
Is there any way I can send slash and backslash keystrokes?
Okay, I have found a solution that may be handy for users working with different keyboard layouts. It uses Alt codes.
public static void alt(int event1, int event2, int event3, int event4) throws Exception {
Robot bot = new Robot();
bot.delay(50); //Optional
bot.keyPress(KeyEvent.VK_ALT);
bot.keyPress(event1);
bot.keyRelease(event1);
bot.keyPress(event2);
bot.keyRelease(event2);
bot.keyPress(event3);
bot.keyRelease(event3);
bot.keyPress(event4);
bot.keyRelease(event4);
bot.keyRelease(KeyEvent.VK_ALT);
}
Then you call it like this:
For back slashes:
alt(KeyEvent.VK_NUMPAD0, KeyEvent.VK_NUMPAD0, KeyEvent.VK_NUMPAD9, KeyEvent.VK_NUMPAD2);
For normal ones:
alt(KeyEvent.VK_NUMPAD0, KeyEvent.VK_NUMPAD0, KeyEvent.VK_NUMPAD4, KeyEvent.VK_NUMPAD7);
No exceptions.
Also works for all other characters. But make sure you have Num Lock on when using this.
(Sorry I should mention this isn't quite an answer, just saying that it does work for me so I'm guessing it's a configuration issue or something--but I thought that it was something others might find handy. It should be a comment but for technical reasons I have to make it an answer)
I hadn't messed with the Robot class and just spent a FUN half hour messing around and bulding some stuff on top of this cool java class.
For me sending slash and backslash work fine. Since the VK_??? stuff maps pretty well to the ASCII characters, you can send '\' or '/' and it should work too.
I used Groovy because that's what I play in these days, but here's a nice example and a bunch of reusable code I just came up with. It's written as a script but could easily be converted to a class in either Groovy or Java (and I will do so soon).
This must be run from a "Priviliged" shell (for instance right-click on the command prompt and select "Run as administrator").
also it MUST have time for you to let go of the keyboard! (Learned that the hard way), so if you use groovyShell and use alt-r to run it, be sure to put a 1 second delay before sending the first keys or your ALT will become part of the keys pressed.
import java.awt.*
import java.awt.event.*
import static java.awt.event.KeyEvent.*
r=new Robot()
r.autoWaitForIdle = true
r.autoDelay=200 // Usually works with 0 but sometimes that's too fast.
// This will alt-tab you to your "Previous" app. While testing I edited this in notepad++
// then tabbed out to a shell to execute it, this tabbed back into my editor and typed
// the "test" text.
alt VK_TAB
send "backslash=\\ \nforward slash =/"
// This will send any string
def send(String s)
{
def difference = ("a" as Character) - ("A" as Character)
s.each {
Character c=it as Character
if(c.isUpperCase()) {
shift c
} else if(c.isLowerCase()) {
send(c - difference)
}
else send(c)
}
}
// These will work for integers and chars, NOT strings
def send(key)
{
press(key as Integer)
release(key as Integer)
}
def alt(key)
{
press VK_ALT
send key
release VK_ALT
}
def shift(key)
{
press VK_SHIFT
send key
release VK_SHIFT
}
def press(key)
{
r.keyPress(key as Integer)
}
def release(int key)
{
r.keyRelease(key as Integer)
}