zoukankan      html  css  js  c++  java
  • windows下自动生成文件夹下所有JNI所需的.h头文件

    以下程序打包成jar后在生成的.class文件的根路径(如elcipse工程的bin文件夹)下运行即可!!
    生成的.h文件放在当前目录的h文件夹下
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    
    public class GenerateHFile {
    	static String suffix = ".class";
    	static String outputFolder = ".\h\";//输出文件夹
    
    	/**生成以classRoot为根文件夹的类的JNI用的.h文件,其中folder为当前路径,他应为classRoot的一个子路径
    	 * @param folder 当前文件夹
    	 * @param classRoot 类文件的根路径
    	 */
    	static void generateHFile(File folder, File classRoot) {
    		File fs[] = folder.listFiles();
    		for (File file : fs) {
    			if (file.isDirectory()) {
    				generateHFile(file, classRoot);
    			} else if (file.getName().endsWith(suffix)) {
    				String exe = "javah";
    				String arg = classRoot.getAbsolutePath();
    				String tmp = file.getAbsolutePath();
    				tmp = tmp.substring(arg.length() + 1,
    						tmp.length() - suffix.length());
    				tmp = tmp.replace(File.separator.charAt(0), '.');
    				String cmds[] = { exe, "-d", outputFolder,"-classpath",arg, tmp };
    				try {
    					Process p = Runtime.getRuntime().exec(cmds);
    					p.waitFor();
    				} catch (IOException e) {
    					e.printStackTrace();
    				} catch (InterruptedException e) {
    					e.printStackTrace();
    				}
    
    				System.out.println(arg);
    			}
    		}
    	}
    
    	static String prefix = "JNIEXPORT";
    
    	/**
    	 * 清除不包含native方法的h文件,根据文件中是否含有"JNIEXPORT"来进行判断
    	 */
    	static void clean() {
    		File[] fs = new File(outputFolder).listFiles();
    		Label: for (int i = 0; i < fs.length; i++) {
    
    			try {
    				BufferedReader br = new BufferedReader(new FileReader(fs[i]));
    
    				while (br.ready()) {
    					String s = br.readLine();
    					if (s.startsWith(prefix)) {
    						continue Label;
    					}
    				}
    				br.close();
    				System.out.print("has deleted successfully: " + fs[i]);
    				boolean b = fs[i].delete();
    				System.out.println(" : " + b);
    
    			} catch (FileNotFoundException e) {
    				e.printStackTrace();
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    		}
    	}
    
    	public static void main(String[] args) {
    		File dir = new File(".");
    		new File(outputFolder).mkdirs();
    		generateHFile(dir, dir);
    		clean();
    
    	}
    }
    


  • 相关阅读:
    Vuex2.0+Vue2.0构建备忘录应用实践
    一步步构造自己的vue2.0+webpack环境
    .NET入行之工作前
    webpack入门之简单例子跑起来
    vue中,class、内联style绑定、computed属性
    wap问答系统工作总结
    ASP.NET Core Api网关Ocelot的中文文档
    在pom.xml中添加Spring依赖
    【java基础】从反射开始(Reflection)
    【java基础】 == 和 equals() 的区别
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13318681.html
Copyright © 2011-2022 走看看