1

In the code below I get an error, and somehow I couldn't find information to fix it. Sorry for any misunderstandings.

import com.sun.jna.Library;
import com.sun.jna.Native; 
import com.sun.jna.platform.win32.Kernel32;
// JNA infrastructure import libs.Kernel32; 
// Proxy interface for kernel32.dll 

public interface JnaTests extends Library {
  public boolean Beep(int FREQUENCY , int DURATION );
  static Kernel32 kernel32 = (Kernel32) Native.loadLibrary("kernel32",   Kernel32.class); 
  static void toMorseCode(String letter) throws Exception { 
  for (byte b : letter.getBytes()) { 
   kernel32.Beep(1200, ((b == '.') ? 50 : 150)); 
   Thread.sleep(50); 
  } 
 } 
 public static void main(String[] args) throws Exception { 
   String helloWorld[][] = { {"....", ".", ".-..", ".-..", "---"}, {".--", "---", ".-.", ".-..", "-.."}}; 
   for (String word[] : helloWorld) { 
    for (String letter : word) { 
     toMorseCode(letter); 
     Thread.sleep(150); 
    } 
    Thread.sleep(350); 
   }
  } 
 }
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
loadP
  • 404
  • 1
  • 4
  • 15

2 Answers2

1

Thanks for the answers.

Finally I found that there should be an Interface (Kernel32) in a separated file.

This was mentioned in the community documentation, however some .dll worked also without Interface e.g. User32.dll .

package com.sun.jna.platform;

import com.sun.jna.Library;


//@author windows-System

public class win32 {

 public interface Kernel32 extends Library {

 boolean Beep(int frequency, int duration); 
 // ... (lines deleted for clarity) ... }   
}

}

Main-file

import com.sun.jna.Library;
import com.sun.jna.Native; 
import com.sun.jna.platform.win32.Kernel32;

// JNA infrastructure import libs.Kernel32; 
// Proxy interface for kernel32.dll 

public class JnaTests {

private static Kernel32 kernel32 = (Kernel32)                    
Native.loadLibrary ("kernel32",   Kernel32.class);

private static void toMorseCode(String letter) throws Exception { 
 for (byte b : letter.getBytes()) { 
  kernel32.Beep(1200, ((b == '.') ? 50 : 150)); 
  Thread.sleep(50); 
 }  
} 

public static void main(String[] args) throws Exception { 
 String helloWorld[][] = { {"....", ".", ".-..", ".-..", "---"}, 
 {".--",  "---", ".-.", ".-..", "-.."}}; 

for (String word[] : helloWorld) { 
 for (String letter : word) { 
  toMorseCode(letter); 
  Thread.sleep(150); 
 } 
 Thread.sleep(350); 
}

} }

loadP
  • 404
  • 1
  • 4
  • 15
0

You are not using the correct name for the Kernel32 class. You've imported it with this line:

import com.sun.jna.platform.win32.Kernel32;

But you're trying to use it with the wrong name:

kernel32.Beep(1200, ((b == '.') ? 50 : 150));

Note the capitalization.

It's worth noting that any packages in the com.sun hierarchy are inherently unsafe to use - they are intended to be completely internal to Java, and aren't meant to be used in your programs. They can change without warning or backwards compatibility, and may have extremely specific undocumented requirements that make them unreliable for you to use.

Beeping, specifically, is a very hardware-and-platform-specific thing, and you're not guaranteed that this code will even work on different Windows systems, to say nothing of other OS's. You're better off playing an actual sound file, since that will work everywhere and give you consistent results. See Java equivalent of C# system.beep? for a more in-depth discussion of what you seem to be after.

Community
  • 1
  • 1
Knetic
  • 2,099
  • 1
  • 20
  • 34
  • Hello, Thank you for the answer. It is just for testing purpose as using JNA. The code is a sample code of an Oracle community document. The import was the only option on netbeans to fix an error. – loadP Apr 23 '16 at 20:20
  • The lowercase is a variable that refers to the loaded library instance and is correct. Further, `com.sun` is used in the JNA project for historical purposes, but has no relation to the "unsafe" Java classes. – Daniel Widdis Jul 21 '22 at 15:07