【问题标题】:JNA beep() cannot find symbol?JNA beep() 找不到符号?
【发布时间】:2016-04-23 19:36:01
【问题描述】:

在下面的代码中,我收到一个错误,不知何故我找不到修复它的信息。如有任何误解,请见谅。

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

【问题讨论】:

    标签: java jna beep


    【解决方案1】:

    您没有为Kernel32 类使用正确的名称。你已经用这一行导入了它:

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

    但您尝试使用错误的名称:

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

    注意大写。

    值得注意的是,com.sun 层次结构中的任何包本质上都是不安全的——它们完全是 Java 内部的,并不打算在您的程序中使用。它们可以在没有警告的情况下更改或向后兼容,并且可能具有极其具体的未记录要求,使您无法可靠地使用它们。

    具体来说,哔声是一种非常特定于硬件和平台的事情,您无法保证此代码甚至可以在不同的 Windows 系统上运行,更不用说其他操作系统了。你最好播放一个实际的声音文件,因为它可以在任何地方工作并给你一致的结果。请参阅Java equivalent of C# system.beep?,以更深入地讨论您的目标。

    【讨论】:

    • 您好,感谢您的回答。它仅用于测试目的,就像使用 JNA 一样。该代码是 Oracle 社区文档的示例代码。导入是 netbeans 上修复错误的唯一选项。
    【解决方案2】:

    感谢您的回答。

    最后我发现在一个单独的文件中应该有一个接口(Kernel32)。

    社区文档中提到了这一点,但是一些 .dll 也可以在没有接口的情况下工作,例如用户32.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) ... }   
    }
    

    }

    主文件

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

    } }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-11
      • 2012-11-14
      • 1970-01-01
      • 2013-09-12
      • 2013-06-06
      • 2017-10-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多