简体   繁体   中英

JNA beep() cannot find symbol?

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); 
   }
  } 
 }

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.

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 eg 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); } } }

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); } } }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM