I got the below error:
Hasans-Air:read-pdf hasan$ java -cp pdf.jar hasan.pdf.MainKt
Hello Kotlin
Exception in thread "main" java.lang.NoClassDefFoundError: kotlin/TypeCastException
at hasan.pdf.MainKt.main(main.kt:5)
at hasan.pdf.MainKt.main(main.kt)
Caused by: java.lang.ClassNotFoundException: kotlin.TypeCastException
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:604)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 2 more
Hasans-Air:read-pdf hasan$
While running the command:
$ java -cp pdf.jar hasan.pdf.MainKt
My Kotlin code is:
// main.kt
package hasan.pdf
fun main(){
println("Hello Kotlin")
ReadPdf.main()
}
And
// ReadPdf.kt
package hasan.pdf
import org.apache.pdfbox.pdmodel.PDDocument
import org.apache.pdfbox.text.PDFTextStripper
import org.apache.pdfbox.text.PDFTextStripperByArea
import java.io.File
import java.io.IOException
object ReadPdf {
@Throws(IOException::class)
@JvmStatic
fun main() {
PDDocument.load(File("HasanResume.pdf")).use { document ->
document.javaClass
if (!document.isEncrypted) {
val stripper = PDFTextStripperByArea()
stripper.sortByPosition = true
val tStripper = PDFTextStripper()
val pdfFileInText = tStripper.getText(document)
//System.out.println("Text:" + st);
// split by whitespace
val lines = pdfFileInText.split("\\r?\\n".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
for (line in lines) {
println(line)
}
}
}
}
}
And
// build.gradle.kts
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
java
kotlin("jvm") version "1.3.50"
}
group = "hasan.pdf"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
}
dependencies {
implementation(kotlin("stdlib-jdk8"))
implementation("org.apache.pdfbox:pdfbox:2.0.17")
testCompile("junit", "junit", "4.12")
}
configure<JavaPluginConvention> {
sourceCompatibility = JavaVersion.VERSION_1_8
}
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "1.8"
}