Java 代码 运行 在 windows 但不在 linux
Java code running on windows but not on linux
我有这段代码,我正在尝试 运行 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.run(URLClassLoader.java:366)
at java.net.URLClassLoader.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
我该如何解决这个问题?
如有任何帮助,我们将不胜感激。
你运气不好。 JACOB 需要一个 jacob.dll
文件,该文件仅适用于 windows.
ActiveX 在 Linux 上不可用,因为它(像 jacob)需要 Windows 二进制文件。我认为即使使用像 Wine 这样的模拟器也无济于事,因为 运行 Excel 在 Wine 上并不是最简单的。
也许最好的方法是切换到独立平台来创建 XLS 文件:https://poi.apache.org/
我没有提供示例,因为我确信使用 google 或 Whosebug 可以找到很多示例。
我有这段代码,我正在尝试 运行 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.run(URLClassLoader.java:366)
at java.net.URLClassLoader.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
我该如何解决这个问题?
如有任何帮助,我们将不胜感激。
你运气不好。 JACOB 需要一个 jacob.dll
文件,该文件仅适用于 windows.
ActiveX 在 Linux 上不可用,因为它(像 jacob)需要 Windows 二进制文件。我认为即使使用像 Wine 这样的模拟器也无济于事,因为 运行 Excel 在 Wine 上并不是最简单的。
也许最好的方法是切换到独立平台来创建 XLS 文件:https://poi.apache.org/
我没有提供示例,因为我确信使用 google 或 Whosebug 可以找到很多示例。