Skip to content

Commit 7b3eb7a

Browse files
committed
Update java usage
1 parent ec9f9e8 commit 7b3eb7a

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-9
lines changed

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -406,11 +406,15 @@ TEN VAD provides comprehensive Java support with JNI (Java Native Interface) bin
406406
#### **Compilation**
407407

408408
```bash
409-
# Compile Java source
410-
javac -cp . include/TenVad.java examples/TestTenVad.java
409+
# Compile Java source, note if JNA library is not installed, you should download it first. For example, you can download JNA library and include it here. You can also export it to the CLASSPATH environment variables
410+
wget https://repo1.maven.org/maven2/net/java/dev/jna/jna/5.13.0/jna-5.13.0.jar # I just picked a random version
411+
javac -encoding UTF-8 -cp jna-5.13.0.jar -d . include/TenVad.java examples/TestTenVad.java
411412

412-
# Run example
413-
java -cp . -Djava.library.path=lib TestTenVad s0724-s0730.wav out.txt
413+
# Run example in the project root directory
414+
java -cp .:jna-5.13.0.jar TestTenVad examples/s0724-s0730.wav examples/out.txt
415+
416+
# Run example in the examples directory
417+
java -cp ..:../jna-5.13.0.jar TestTenVad s0724-s0730.wav out.txt
414418
```
415419

416420
#### **Example Code**

examples/TestTenVad.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//
77

88

9-
// import com.ten.vad.TenVad; // Uncomment when using package structure
9+
import com.ten.vad.TenVad; // Comment when not using package structure
1010
import javax.sound.sampled.*;
1111
import java.io.*;
1212
import java.nio.ByteBuffer;

include/TenVad.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,27 @@ private static String getLibPath() {
7676
String osName = System.getProperty("os.name").toLowerCase();
7777
String arch = System.getProperty("os.arch").toLowerCase();
7878
try {
79+
// Get the directory where the class file is located
7980
String currentDir = Paths.get(TenVad.class.getProtectionDomain()
80-
.getCodeSource().getLocation().toURI()).getParent().toString();
81-
if (osName.contains("linux")) return Paths.get(currentDir, "lib", "Linux", "x64", "libten_vad.so").toString();
82-
if (osName.contains("windows")) return Paths.get(currentDir, "lib", "Windows", (arch.contains("64") ? "x64" : "x86"), "ten_vad.dll").toString();
83-
if (osName.contains("mac")) return Paths.get(currentDir, "lib", "macOS", "ten_vad.framework", "ten_vad").toString();
81+
.getCodeSource().getLocation().toURI()).toString();
82+
83+
// If currentDir points to a .class file or directory, navigate up to project root
84+
// Handle both packaged (jar) and unpackaged (file system) cases
85+
File baseDir = new File(currentDir);
86+
if (baseDir.isFile()) {
87+
baseDir = baseDir.getParentFile(); // Go up from jar file
88+
}
89+
// If we're in a package structure (com/ten/vad), go up to project root
90+
while (baseDir != null && !new File(baseDir, "lib").exists()) {
91+
baseDir = baseDir.getParentFile();
92+
}
93+
94+
if (baseDir != null && new File(baseDir, "lib").exists()) {
95+
String projectRoot = baseDir.getAbsolutePath();
96+
if (osName.contains("linux")) return Paths.get(projectRoot, "lib", "Linux", "x64", "libten_vad.so").toString();
97+
if (osName.contains("windows")) return Paths.get(projectRoot, "lib", "Windows", (arch.contains("64") ? "x64" : "x86"), "ten_vad.dll").toString();
98+
if (osName.contains("mac")) return Paths.get(projectRoot, "lib", "macOS", "ten_vad.framework", "Versions", "A", "ten_vad").toString();
99+
}
84100
} catch (Exception ignore) {}
85101
return "ten_vad";
86102
}

0 commit comments

Comments
 (0)