Browse Source

Library in jar now

Thomas Buck 12 years ago
parent
commit
40aa3b36cf
3 changed files with 79 additions and 15 deletions
  1. 66
    2
      CubeControl/HelperUtility.java
  2. 9
    9
      CubeControl/makefile
  3. 4
    4
      README.md

+ 66
- 2
CubeControl/HelperUtility.java View File

22
  */
22
  */
23
 
23
 
24
 /**
24
 /**
25
- * Helper class which runs our native library.
25
+ * Helper class which runs our native library, which is loaded from inside the Jar.
26
  * @author Thomas Buck
26
  * @author Thomas Buck
27
  * @author Max Nuding
27
  * @author Max Nuding
28
  * @author Felix Bäder
28
  * @author Felix Bäder
29
  * @version 1.0
29
  * @version 1.0
30
  */
30
  */
31
 
31
 
32
+import java.io.*;
33
+import java.nio.channels.*;
34
+import java.nio.*;
35
+
32
 public class HelperUtility {
36
 public class HelperUtility {
33
 
37
 
38
+	// Load libraries, copy from Jar if needed
39
+	// Mostly from:
40
+	// http://stackoverflow.com/questions/1611357/how-to-make-a-jar-file-that-include-dll-files
34
 	static {
41
 	static {
35
-		System.loadLibrary("Serial");
42
+		// System.out.println("Loading Serial Library...");
43
+		loadFromJar();
44
+	}
45
+
46
+	/**
47
+	 * When packaged into JAR extracts DLLs, places these into
48
+	 */
49
+	private static void loadFromJar() {
50
+		// we need to put DLL to temp dir
51
+		String os = System.getProperty("os.name").toLowerCase();
52
+		String path = "CC_";
53
+		if (os.indexOf("windows") > -1) {
54
+			loadLib(path, "Serial.dll");
55
+		} else if (os.indexOf("mac") > -1) {
56
+			loadLib(path, "libSerial.jnilib");
57
+		}
58
+	}
59
+
60
+	/**
61
+	 * Puts library to temp dir and loads to memory
62
+	 */
63
+	private static void loadLib(String path, String name) {
64
+		try {
65
+			// have to use a stream
66
+			InputStream in = HelperUtility.class.getResourceAsStream(name);
67
+			// System.out.println("Input Stream: " + in);
68
+			File fileOut = new File(System.getProperty("java.io.tmpdir") + "/" + path + name);
69
+			OutputStream out = new FileOutputStream(fileOut);
70
+			ReadableByteChannel inChannel = Channels.newChannel(in);
71
+			WritableByteChannel outChannel = Channels.newChannel(out);
72
+			fastChannelCopy(inChannel, outChannel);
73
+			inChannel.close();
74
+			outChannel.close();
75
+			System.load(fileOut.toString());
76
+		} catch (Exception e) {
77
+			System.out.println("Failed to load Serial Library:");
78
+			e.printStackTrace();
79
+		}
80
+	}
81
+
82
+	// http://thomaswabner.wordpress.com/2007/10/09/fast-stream-copy-using-javanio-channels/
83
+	public static void fastChannelCopy(ReadableByteChannel src, WritableByteChannel dest) throws IOException {
84
+		ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024);
85
+		while (src.read(buffer) != -1) {
86
+			// prepare the buffer to be drained
87
+			buffer.flip();
88
+			// write to the channel, may block
89
+			dest.write(buffer);
90
+			// If partial transfer, shift remainder down
91
+			// If buffer is empty, same as doing clear()
92
+			buffer.compact();
93
+		}
94
+		// EOF will leave buffer in fill state
95
+		buffer.flip();
96
+		// make sure the buffer is fully drained.
97
+		while (buffer.hasRemaining()) {
98
+			dest.write(buffer);
99
+		}
36
 	}
100
 	}
37
 
101
 
38
 	/**
102
 	/**

+ 9
- 9
CubeControl/makefile View File

3
 # Path to jni.h
3
 # Path to jni.h
4
 ifdef SystemRoot
4
 ifdef SystemRoot
5
 HEADERPATH = C:/Program\ Files/Java/jdk1.6.0_29/include
5
 HEADERPATH = C:/Program\ Files/Java/jdk1.6.0_29/include
6
-$(RM) = del
6
+RM = del
7
+INJAR = *.class *.png Serial.dll
8
+PLATFORM = Win
7
 else
9
 else
8
 HEADERPATH = /System/Library/Frameworks/JavaVM.framework/Headers
10
 HEADERPATH = /System/Library/Frameworks/JavaVM.framework/Headers
9
-$(RM) = rm -f
11
+RM = rm -f
12
+INJAR = *.class *.png libSerial.jnilib
13
+PLATFORM = Mac
10
 endif
14
 endif
11
 
15
 
12
 # All java files to be compiled
16
 # All java files to be compiled
13
 # List so it works as target
17
 # List so it works as target
14
 JAVAFILES = HelperUtility.java AnimationUtility.java Animation.java AFrame.java cubeWorker.java layerEditFrame.java Led3D.java Frame.java
18
 JAVAFILES = HelperUtility.java AnimationUtility.java Animation.java AFrame.java cubeWorker.java layerEditFrame.java Led3D.java Frame.java
15
 
19
 
16
-# Files that go in the jar
17
-INJAR = *.class *.png
18
-
19
 # --------------------------------------
20
 # --------------------------------------
20
 
21
 
21
-all: libSerial CubeControl.jar
22
+all: libSerial CubeControl.jar clean
22
 
23
 
23
 doc: doc/index.html
24
 doc: doc/index.html
24
 
25
 
25
-CubeControl.jar: HelperUtility.class manifest.txt
26
-	jar -cmf manifest.txt "CubeControl.jar" $(INJAR)
26
+CubeControl.jar: HelperUtility.class manifest.txt libSerial
27
+	jar -cmf manifest.txt "CubeControl$(PLATFORM).jar" $(INJAR)
27
 
28
 
28
 serialInterface.h: HelperUtility.class
29
 serialInterface.h: HelperUtility.class
29
 	javah -o serialInterface.h HelperUtility
30
 	javah -o serialInterface.h HelperUtility
54
 	$(RM) *.class
55
 	$(RM) *.class
55
 	$(RM) *.h
56
 	$(RM) *.h
56
 	$(RM) *.o
57
 	$(RM) *.o
57
-	$(RM) *.jar
58
 ifdef SystemRoot
58
 ifdef SystemRoot
59
 	$(RM) *.dll
59
 	$(RM) *.dll
60
 else
60
 else

+ 4
- 4
README.md View File

13
 
13
 
14
 ## Cube Control
14
 ## Cube Control
15
 
15
 
16
-We also build a software to create and load animations into the cube. This software is written in Java and C and is Compatible between Windows and Unix.
17
-It's source is in the "Cube Control" directory. It has it's own makefile, in which you can specify the Target OS. "unix" means any Unix like OS (Linux, OS X...), "win" means Windows.
18
-You obviously need a working JDK and a C Compiler Environment (we use gcc).
16
+We also build a software to create and load animations into the cube. This software is written in Java and C and should work on Windows and Unix.
17
+It's source is in the "Cube Control" directory.
19
 
18
 
20
 ## Build instructions
19
 ## Build instructions
21
 
20
 
22
-Theres a global makefile in the top folder. If you run it, it will (probably) create CubeControl.jar, CubeFirmware.hex and a OS-dependent serial library (Serial.dll or libSerial.jnilib...). If not, you should take a look at the makefile in CubeControl. Hard-Coded include directorys are probably different than on your system...
21
+Theres a global makefile in the top folder. If you run it, it will (probably) create CubeControl.jar, CubeFirmware.hex and a OS-dependent serial library (Serial.dll or libSerial.jnilib...). If not, you should take a look at the makefile in CubeControl. Hard-Coded include directorys are probably different than on your system... You obviously need a working JDK and a C Compiler Environment (we use gcc).
22
+CubeControls makefile will autodetect a Windows Host and compile a Windows Version accordingly. If it is not on Windows, it will compile a Mac Version. Unix is currently not supported by the makefile, but should work if compiled manually.

Loading…
Cancel
Save