37,719
社区成员
发帖
与我相关
我的任务
分享
package com;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
public class test {
public String run(){
return "success";
}
public String run(String fileLnkName){
return fileLnkName;
}
public static String getLnkFile(File lnkFile) {
RandomAccessFile r = null;
ByteArrayOutputStream bos = null;
String filename = null;
try {
r = new RandomAccessFile(lnkFile, "r");
byte[] bys = new byte[4];
// 定位到 Shell item ID list 段的长度定义起始地址
// 以便于计算下一段(即文件位置信息段的起始地址)
r.seek(0x4c);
r.read(bys, 0, 2);
int offset = bytes2Int(bys, 0, 2);
// 获得文件位置信息段的起始地址
int fileLocationInfoSagement = offset + 0x4e;
// 获得本地路径信息段的起始地址
int filePathInfoSagement = fileLocationInfoSagement + 0x10;
// 定位到本地路径信息段,以便获得本地路径信息的偏移地址
r.seek(filePathInfoSagement);
// 获得本地路径信息的偏移
r.read(bys, 0, 4);
int filePathInfoOffset = fileLocationInfoSagement + bytes2Int(bys, 0, 4);
// 定位到本地路径信息起始地址,开始读取路径信息
r.seek(filePathInfoOffset);
bos = new ByteArrayOutputStream();
for(byte b = 0; (b = r.readByte()) != 0;) {
bos.write(b);
}
// 将读出路径信息字节存入 byte 数组中
bys = bos.toByteArray();
// 采用本地编码获得路径信息文件名称
filename = new String(bys);
} catch (IOException e) {
e.printStackTrace();
} finally {
if(bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(r != null) {
try {
r.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return filename;
}
public static int bytes2Int(byte[] bys, int start, int len) {
int n = 0;
for(int i = start, k = start + len % 5; i < k; i++) {
n += (bys[i] & 0xff) << (i * 8);
}
return n;
}
}
python调用java代码如下# -*- coding:utf8 -*-
import jpype
import os
#jvmpath是java虚拟机的位置
jarpath = os.path.join(os.path.abspath('.'), 'E:/Symantec/')
jpype.startJVM(jpype.getDefaultJVMPath(), "-Djava.ext.dirs=%s" % jarpath)
res = None
test = jpype.JClass('com.test')#com就是test所在包的位置 test在com包中
t = test()
#res = t.getLnkFile("C:\\Users\\aushelp5\\Desktop\\ASP.NET 3.5从入门到精通 - 快捷方式.lnk")
res=t.run('zhagnsan')#这句是调用test中run的重载方法 测试过是可以输出来结果的 也就是可以带参数
print res
public static String getLnkFile(String file_path) {
File lnkFile = new File(file_path);
}
//我现在穿进来的是string 然后再方法里面在穿进去file里面就ok了 测试了下没问题
public static void main(String[] args) {
test tt=new test();
System.out.print(tt.getLnkFile("C:\\Users\\aushelp5\\Desktop\\test.lnk"));
}
输出结果是 D:\test.xls
可是在python调用这个方法传进去一个字符串还是报错 而且错误还是一样
Traceback (most recent call last):
File "E:\Symantec\python_java.py", line 10, in <module>
res = t.getLnkFile("C:\\Users\\aushelp5\\Desktop\\test.lnk")
RuntimeError: No matching overloads found. at src/native/common/jp_method.cpp:121
可是我python调用还是报错# -*- coding:utf8 -*-
import jpype
import os
#jvmpath是java虚拟机的位置
jarpath = os.path.join(os.path.abspath('.'), 'E:/Symantec/')
jpype.startJVM(jpype.getDefaultJVMPath(), "-Djava.ext.dirs=%s" % jarpath)
res = None
test = jpype.JClass('com.test')#com就是test所在包的位置 test在com包中
t = test()
#res = t.getLnkFile("C:\\Users\\aushelp5\\Desktop\\ASP.NET 3.5从入门到精通 - 快捷方式.lnk")
res=t.run('zhagnsan')#这句是调用test中run的重载方法 测试过是可以输出来结果的 也就是可以带参数
print res
res=t.run('zhagnsan')测试没问题 可以调用到jar里面test类中run带参数的方法 而且输出ok
res = t.getLnkFile("C:\\Users\\aushelp5\\Desktop\\ASP.NET 3.5从入门到精通 - 快捷方式.lnk")#这个失败
哎