我有一个项目,我需要有它的jar文件,但它没有打开,任何人都可以帮助我,我需要尽快为我的大学。以下是整个代码:
package huffmanproject;
import java.util.ArrayList;
import java.util.PriorityQueue;
public class huffmanproject {
public static class HuffNode implements Comparable<HuffNode> {
public int value;
public int weight;
public HuffNode leftTree;
public HuffNode rightTree;
public HuffNode parent;
public HuffNode() {
parent = null;
}
public HuffNode( int v, int w, HuffNode lTree, HuffNode rTree, HuffNode par ) {
value = v;
weight = w;
leftTree = lTree;
rightTree = rTree;
parent = par;
}
@Override
public int compareTo(HuffNode rhs) {
return weight - rhs.weight;
}
@Override
public String toString() {
String str = "";
str += this.value;
return str;
}
}
public static class HuffTree {
private int size = 0;
private HuffNode root = new HuffNode();
private PriorityQueue<HuffNode> huffQueue = new PriorityQueue();
public ArrayList<String> pathTable = new ArrayList();
public ArrayList<Character> valueTable = new ArrayList();
public HuffTree(int[] freq, char[] code) {
this.size = freq.length;
if (freq.length != code.length) {
throw new UnsupportedOperationException("Error: Character and code length mismatch.");
}
for (int i = 0; i < this.size; i++) {
huffQueue.offer(new HuffNode(code[i], freq[i], null, null, null));
}
createTree();
createTable(this.root, "");
}
private void createTree() {
while (huffQueue.size() > 1) {
HuffNode tempL = huffQueue.poll();
HuffNode tempR = huffQueue.poll();
HuffNode parent = new HuffNode(0, tempL.weight+tempR.weight, tempL, tempR, null);
tempL.parent = parent;
tempR.parent = parent;
huffQueue.offer(parent);
this.size++;
}
this.root = huffQueue.peek();
}
private void createTable(HuffNode curr, String str) {
if (curr == null) return;
if (curr.leftTree == null && curr.rightTree == null) {
char tempChar;
if (curr.value == 32)
tempChar = ' ';
if (curr.value == 10)
tempChar = 'n';
else
tempChar = (char)curr.value;
this.valueTable.add(tempChar);
this.pathTable.add(str);
}
str += "0";
createTable(curr.leftTree, str);
str = str.substring(0, str.length()-1);
str += "1";
createTable(curr.rightTree, str);
}
String tacks = "";
public void getTree(HuffNode curr) {
if (curr == null) return;
if (curr.leftTree == null && curr.rightTree == null) {
switch (curr.value) {
case 32:
System.out.println(tacks + curr.weight + ": sp");
break;
case 10:
System.out.println(tacks + curr.weight + ": nl");
break;
default:
System.out.println(tacks + curr.weight + ": " + (char)curr.value);
break;
}
}
else
System.out.println(tacks + curr.weight);
tacks += "- ";
getTree(curr.leftTree);
getTree(curr.rightTree);
tacks = tacks.substring(0, tacks.length()-2);
}
public int getSize() { return this.size; }
public String encode(String input){
String str = "";
for (int x = 0; x < input.length(); x++) {
for (int i = 0; i < valueTable.size(); i++) {
if (valueTable.get(i) == input.charAt(x))
str += pathTable.get(i);
}
}
return str;
}
public String decode(String bits) {
String decodedStr = "";
for (int i = 0; i < bits.length(); i++) {
if (!getChar(bits.substring(0, i+1)).equals("")) {
decodedStr += getChar(bits.substring(0, i+1));
bits = bits.substring(i+1);
i = 0;
}
}
return decodedStr;
}
private String getChar(String bits) {
String character = "";
for (int i = 0; i < pathTable.size(); i++) {
if (pathTable.get(i).equals(bits))
character = valueTable.get(i).toString();
}
return character;
}
}
public static void main(String[] args) {
// for example assume that we have these letters with these different frequencies like below:
int freq[] = {10, 15, 12, 3, 4, 13, 1};
char code[] = {'a', 'e', 'i', 's', 't', ' ', '\n'};
HuffTree hTree = new HuffTree(freq, code);
System.out.println("Display Tree:");
HuffNode curr = hTree.root;
hTree.getTree(curr);
System.out.println("");
// and we want to build the huffman tree of the word sea :
System.out.println("Encode 'sea': " + hTree.encode("sea") +"\n");
System.out.println("Decode '" + hTree.encode("sea") + "': " + hTree.decode(hTree.encode("tea")));
}
}
答案 0 :(得分:0)
如果它只是没有编译成jar文件,请在命令提示符或终端中尝试以下命令。
jar cf jar-file input-file(s)
来自Oracle:Creating jar File
要打开命令提示符,请使用WIN + R打开运行框,键入cmd,然后按Enter键。
导航到java文件的目录:
cd C:\Path\to\my\java\file\HuffNode.java
运行命令:
jar cf HuffNode.jar HuffNode.java
如果您有多个.java文件:
jar cf HuffNode.jar File1.java File2.java File3.java