在执行可运行的jar文件(java代码)时,我收到java.lang.reflect.InvocationTargetException

时间:2015-08-17 15:52:13

标签: java excel executable-jar runnable read-write

作为我的一个项目任务的一部分,我需要从excel读取一些数据,通过Sikuli使用这些数据处理应用程序,然后将结果写回Excel。

它在Eclipse中运行良好。但是当我将Java文件导出到Runnable jar文件时,我收到一个错误(堆栈跟踪位于这篇文章的底部)。谁能帮我解决这个问题?

请在下面找到我的Java代码:

package Auto_a;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.sikuli.script.FindFailed;
import org.sikuli.script.Key;
import org.sikuli.script.Screen;

public class Calculator_b {

	public static String[] temp1 = new String[50];
	public static String temp,temp2;
	public static String var1,var2,var3,var4,var5,var6,var7,var8,var9,var10,var11,var12;
	public static String var13,var14,var15,var16,var17,var18,var19,var20;
	public static String str5;
	public static String result,result_a,result_b;
	public static String[] temp_var;
	public static String result1,result2;
	public static int rowNum,colNum;
	public static String[] temp_var2 ;
	public static int x=0,flag=0;
	public static void main(String...strings) throws IOException, InterruptedException, FindFailed
    {    
		
		 Screen s=new Screen();		 
		 Thread.sleep(2000);	
		 
		 s.find("D:\\Sikuli\\Calculator.PNG");    
		 Thread.sleep(500);
		 s.click("D:\\Sikuli\\Calculator.PNG");   
		 Thread.sleep(2000);		
		 
		 Calculator_b objExcelFile = new Calculator_b();       

			String filePath = "D:";
			//Call read file method of the class to read data       
			objExcelFile.readExcel(filePath,"sample.xlsx","Calculator");
		 
    }
	
	
	public void readExcel(String filePath,String fileName,String sheetName) throws IOException, FindFailed, InterruptedException
	{       

		//Create a object of File class to open xlsx file       
		File file =    new File(filePath+"\\"+fileName);       
		//Create an object of FileInputStream class to read excel file      
		FileInputStream fis = new FileInputStream(file);  
		
		 XSSFWorkbook wb = new XSSFWorkbook(fis);
		
		//Read sheet inside the workbook by its name       
		//Sheet guru99Sheet = wb.getSheet(sheetName);   
		
	    XSSFSheet ws = wb.getSheet(sheetName);

	    rowNum = ws.getLastRowNum() + 1;
	    System.out.println(rowNum);
	    colNum = ws.getRow(0).getLastCellNum();
	    System.out.println(colNum);
	    String[][] data = new String[rowNum][colNum];
	    temp_var2 = new String[colNum+1];

	    for (int i = 1 ; i < rowNum ; i++) 
	    {	
	        XSSFRow row = ws.getRow(i);
	            for (int j = 1 ; j < colNum ; j++) 
	            {
	                XSSFCell cell = row.getCell(j);
	                String value = cellToString(cell);
	               
	                data[i][j] = value ;	                
	            }		            	            
	         
	     }
	    
	    //New Loop to assign data to Temp array
	    
	    for(int i1=1;i1 < rowNum ; i1++)
	    {	    	
	    	for (int j1 = 1 ; j1 < colNum ; j1++) 
            {
	    		x=j1;
	    		temp_var2[x]=data[i1][j1];	  	    		
	    		System.out.println("value of temp_var2["+x +"]: "+ temp_var2[x]);
	    		x=x+1;
            }
	    	Calculator_a(i1);	    	
	    }	    
	 
	    int len111 = temp_var2.length;
	    System.out.println("array len: "+len111); 
	     
	}     
	
	 public static String cellToString(XSSFCell cell) 
	    {  
	        int type;
	        Object result;
	        type = cell.getCellType();

	        switch (type) 
	        {

	            case Cell.CELL_TYPE_NUMERIC: // numeric value in Excel
	            case Cell.CELL_TYPE_FORMULA: // precomputed value based on formula
	                result = cell.getNumericCellValue();
	                break;
	            case Cell.CELL_TYPE_STRING: // String Value in Excel 
	                result = cell.getStringCellValue();
	                break;
	            case Cell.CELL_TYPE_BLANK:
	                result = "";
	            case Cell.CELL_TYPE_BOOLEAN: //boolean value 
	                result = cell.getBooleanCellValue();
	                break;
	            case Cell.CELL_TYPE_ERROR:
	            default:  
	                throw new RuntimeException("There is no support for this type of cell");                        
	        }

	        return result.toString();
	    }
	 
	 public static void WriteExcel(int var_c,String str1,String Result) 
	    { 
		 int var_d =var_c;
		 String Actual = str1;
		 String status = Result;
		 try {
			    FileInputStream file = new FileInputStream(new File("D:\\sample_h.xlsx"));
			 
			    XSSFWorkbook workbook = new XSSFWorkbook(file);
			    XSSFSheet sheet = workbook.getSheet("Calculator");
			    Cell cell = null;
			 
			    //Update the value of cell
			    cell = sheet.getRow(var_d).getCell(5);			   
			    cell.setCellValue(Actual);
			    
			    cell = sheet.getRow(var_d).getCell(6);			   
			    cell.setCellValue(status);			    

		    //file.close();
			     
			    FileOutputStream outFile =new FileOutputStream(new File("D:\\sample_h.xlsx"));
			    workbook.write(outFile);
			    //outFile.close();
			     
			} catch (FileNotFoundException e) {
			    e.printStackTrace();
			} catch (IOException e) {
			    e.printStackTrace();
			}
		}
	 
	 public static void Calculator_a(int v_a)throws FindFailed, InterruptedException 
		{
			int v_b=v_a;				
		
			 Screen s=new Screen();		 
			 Thread.sleep(2000);			 
		 					
			 s.find("D:\\Sikuli\\Calculator_c.PNG");    
			 Thread.sleep(1000);
			 s.click("D:\\Sikuli\\Calculator_c.PNG");   		
			 Thread.sleep(3000);
			 
			 s.type(temp_var2[1]);
			 Thread.sleep(1000);
	
			 s.type(temp_var2[3]);			 
			 Thread.sleep(1000);
			 
			 s.type(temp_var2[2]);
			 Thread.sleep(1000);
			 
			 s.type(Key.ENTER);
			 Thread.sleep(1000);
			 
			 String text1;
			 text1=s.find("D:\\Sikuli\\Calculator_b.PNG").above(30).text();
			 text1=text1.trim();
			 System.out.println("Actual Result: "+text1);
			 Thread.sleep(1000);
			 String expected = temp_var2[4];
			 if(expected.equals(text1))
			 {
				 result1="Passed";
				 WriteExcel(v_b,text1,result1);				 
			 }
			 
			 else
			 {
				 result1="Failed";
				 WriteExcel(v_b,text1,result1);					 
			 }
							  	
		}
	}

Error details:

*****sys-package-mgr*: processing new jar, 'D:\testa.jar'
Exception in thread "main" java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoa
der.java:56)
Caused by: Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named sikuli****

at org.python.core.PyException.fillInStackTrace(PyException.java:70)
        at java.lang.Throwable.<init>(Throwable.java:181)
        at java.lang.Exception.<init>(Unknown Source)
        at java.lang.RuntimeException.<init>(Unknown Source)
        at org.python.core.PyException.<init>(PyException.java:46)
        at org.python.core.PyException.<init>(PyException.java:43)
        at org.python.core.PyException.<init>(PyException.java:61)
        at org.python.core.Py.ImportError(Py.java:290)
        at org.python.core.imp.import_first(imp.java:750)
        at org.python.core.imp.import_name(imp.java:834)
        at org.python.core.imp.importName(imp.java:884)
        at org.python.core.ImportFunction.__call__(__builtin__.java:1220)
        at org.python.core.PyObject.__call__(PyObject.java:357)
        at org.python.core.__builtin__.__import__(__builtin__.java:1173)
        at org.python.core.imp.importFromAs(imp.java:978)
        at org.python.core.imp.importFrom(imp.java:954)
        at org.python.pycode._pyx0.f$0(<string>:1)
        at org.python.pycode._pyx0.call_function(<string>)
        at org.python.core.PyTableCode.call(PyTableCode.java:165)
        at org.python.core.PyCode.call(PyCode.java:18)
        at org.python.core.Py.runCode(Py.java:1261)
        at org.python.core.Py.exec(Py.java:1305)
        at org.python.util.PythonInterpreter.exec(PythonInterpreter.java:206)
        at org.sikuli.script.Region.toJythonRegion(Region.java:993)
        at org.sikuli.script.Region.create(Region.java:130)
        at org.sikuli.script.Region.above(Region.java:331)
        at Auto_a.Calculator_b.Calculator_a(Calculator_b.java:192)
        at Auto_a.Calculator_b.readExcel(Calculator_b.java:98)
        at Auto_a.Calculator_b.main(Calculator_b.java:47)
        ... 5 more

我觉得在将数据写入Excel /从Excel读取数据时会出现问题。你能建议任何解决方案吗?

我已经以正确的方式将java代码导出到jar文件中。我通过导出另一个示例程序证实了这一点,并且它工作得很好。此问题仅发生在涉及Excel Read&amp; Excel写入操作

1 个答案:

答案 0 :(得分:0)

我认为这是你的问题:

Caused by: Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named sikuli****

似乎未能导入sikuli。