Java代码在Windows上运行但不在Linux上运行

时间:2015-06-11 07:58:10

标签: java windows excel activex

我有这个代码,我试图在Windows上运行一些代码,运行正常,因为我得到了所需的输出

package com.org.jato.expressionTransform;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

public class ExpressionTransformerImpl {

    private String delimiterForForumal = "_V_";

    private String delimiterForId = "_VSCAM_";

    private int rowLimit = 50000;

    private Set<String> usedCellIdsSet = new HashSet();

    public void convertCurlyBraceToOrCondition(String formula, Long customerId,
            Long userId) {

    }

    public List<String> DelimitFormula(String formula, Long loggedInUserId,
            Long customerId) {
        List<String> updatedForumala = new ArrayList<String>();

        StringTokenizer tokenizer = new StringTokenizer(formula,
                delimiterForForumal);

        while (tokenizer.hasMoreTokens()) {
            updatedForumala.add(tokenizer.nextToken());
        }

        return updatedForumala;

    }

    public void createExecutableForumalaFromString(String forumlaString,
            Long customerId, Long loggedInUserId) {

        Set<String> idString = getIdsFromFormula(forumlaString);

        Map<String, Long> mappingIDItemIdMap = getItemIDsFromIdList(idString,
                1L, 2L);
        Set<Long> itemIdSet = new HashSet();
        for (Long val : mappingIDItemIdMap.values()) {
            itemIdSet.add(val);

        }
        Map<Long, Long> itemDataTypeMap = getDataTypeForItem(itemIdSet);

        Boolean isValid = isValidFormula(forumlaString, itemDataTypeMap, mappingIDItemIdMap);

        System.out.println(isValid);
    }

    private Set<String> getIdsFromFormula(String formulaString) {
        Set<String> idList = new HashSet();

        StringTokenizer idTokens = new StringTokenizer(formulaString,
                delimiterForId);

        while (idTokens.hasMoreTokens()) {
            idTokens.nextToken();

            if (idTokens.hasMoreTokens()) {
                idList.add(idTokens.nextToken());
            }
        }

        return idList;
    }

    private Map<String, String> getCellNamesForIDs(Set<String> idStrings) {
        Map<String, String> idCellMap = new HashMap();

        int rowNum = 1;
        int colNum = 0;
        String array[] = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
                "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
                "W", "X", "Y", "Z" };

        List<String> columnLIst = new ArrayList<String>(Arrays.asList(array));

        for (String id : idStrings) {

            String key = columnLIst.get(colNum) + rowNum;
            if (!usedCellIdsSet.contains(key)) {
                idCellMap.put(key, id);
                usedCellIdsSet.add(key);
            }
            rowNum++;
            if (rowNum == rowLimit) {
                rowNum = 1;
                colNum = colNum + 1;
            }

        }

        return idCellMap;
    }

    private Map<String, Long> getItemIDsFromIdList(Set<String> idList,
            Long speedWingcustId, Long loggedInUserId) {
        Map<String, Long> map = new HashMap();
        for (String id : idList) {
            Long var = Long.valueOf(id) % 5L;
            if (var == 0L) {
                map.put(id, 5L);

            } else {
                map.put(id, var);
            }

        }
        return map;
    }

    private Map<Long, Long> getDataTypeForItem(Set<Long> itemIDList) {
        Map<Long, Long> map = new HashMap();
        for (Long id : itemIDList) {
            Long var = Long.valueOf(id) % 5L;
            if (var == 0L) {
                map.put(id, 5L);

            } else {
                map.put(id, var);
            }

        }
        return map;

    }

    public Boolean isValidFormula(String formulaString,
            Map<Long, Long> itemDataMap, Map<String, Long> itemAttributeIdMap) {
        Boolean isValid = Boolean.FALSE;

        for (String mappingID : itemAttributeIdMap.keySet()) {
            formulaString = formulaString.replaceAll(delimiterForId + mappingID
                    + delimiterForId, itemAttributeIdMap.get(mappingID)
                    .toString());
        }

        for (Long itemID : itemDataMap.keySet()) {
            formulaString = formulaString.replaceAll(itemID + ".value",
                    getReplacement(itemDataMap.get(itemID)));
            formulaString = formulaString.replaceAll(itemID + ".availability",
                    "S");
        }

        System.out.println(formulaString);
        isValid = getDummyResultForFormul(formulaString);
        return isValid;
    }

    private String getReplacement(Long dataType) {
        switch (dataType.intValue()) {
        case 1:
            return "1";
        case 2:
            return "A";
        default:
            break;
        }
        return null;

    }

    private boolean getDummyResultForFormul(String formula) {
        Boolean isValid = Boolean.TRUE;
        Object workbook = null;
        Variant f = null;

        ActiveXComponent xl = new ActiveXComponent("Excel.Application");
        Object xlo = xl.getObject();
        try {
            xl.setProperty("Visible", new Variant(true));
            Object workbooks = xl.getProperty("Workbooks").toDispatch();
            workbook = Dispatch.get((Dispatch) workbooks, "Add").toDispatch();
            Object sheet = Dispatch.get((Dispatch) workbook, "ActiveSheet")
                    .toDispatch();

            Object a1 = Dispatch.invoke((Dispatch) sheet, "Range",
                    Dispatch.Get, new Object[] { "A1" }, new int[1])
                    .toDispatch();
            Dispatch.put((Dispatch) a1, "Formula", formula);
            String val = Dispatch.get((Dispatch) a1, "Value").toString();
            f = new Variant(false);
            Dispatch.call((Dispatch) workbook, "Close", f);
            if(val.equals("null"))
            {
                isValid = Boolean.FALSE;
            }
        } catch (Exception e) {
            e.printStackTrace();
            isValid = Boolean.FALSE;
            f = new Variant(false);
            Dispatch.call((Dispatch) workbook, "Close", f);
        } finally {
            xl.invoke("Quit", new Variant[] {});
        }

        return isValid;
    }

    public static void main(String args[]) {

        String formula = "=IF(_VSCAM_1111_VSCAM_.value<5,IF(_VSCAM_2222_VSCAM_.value={\"high mech\",\"low mech\",\"mid mech\"},10,IF(_VSCAM_2222_VSCAM_.value=\"electronic\",20,0)),IF(_VSCAM_1111_VSCAM_.value=5,IF(_VSCAM_2222_VSCAM_.value=\"electronic\",40,0),0))";

        ExpressionTransformerImpl test = new ExpressionTransformerImpl();

        test.createExecutableForumalaFromString(formula, 1L, 2L);

    }
}

在linux上运行时,我遇到了错误。

Exception in thread "main" java.lang.NoClassDefFoundError: com/jacob/activeX/ActiveXComponent
        at ExpressionTransformerImpl.getDummyResultForFormul(ExpressionTransformerImpl.java:183)
        at ExpressionTransformerImpl.isValidFormula(ExpressionTransformerImpl.java:161)
        at ExpressionTransformerImpl.createExecutableForumalaFromString(ExpressionTransformerImpl.java:60)
        at ExpressionTransformerImpl.main(ExpressionTransformerImpl.java:221)
Caused by: java.lang.ClassNotFoundException: com.jacob.activeX.ActiveXComponent
        at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:358)

编译这个,我使用下面的命令

 javac -cp  jacob.jar ExpressionTransformerImpl.java

并运行它,我正在使用此命令

java ExpressionTransformerImpl

如何解决此问题?

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:3)

你运气不好。 JACOB需要jacob.dll文件,该文件仅适用于Windows。

答案 1 :(得分:0)

ActiveX在Linux上不可用,因为它(如jacob)需要Windows二进制文件。我认为即使使用像Wine这样的模拟器也无济于事,因为在Wine上运行Excel并不是最简单的。

也许最好的方法是切换到独立的平台来创建XLS文件:https://poi.apache.org/

我没有提供示例,因为我确信使用google或stackoverflow可以找到很多内容。