我想将一个小型 Java 应用程序作为可运行的 jar 移交,但我不希望任何人访问我的源代码。我是否正确地假设 jar 文件中不包含源代码(.java 文件)?
用户269799
我可以在没有主类的情况下以这种方式执行 jar 文件吗:
java -jar my_jar.jar -getX arg1 arg2 ...
Run Code Online (Sandbox Code Playgroud)
知道我有一个名为 getX 的方法,它以 arg1 arg2 ... 作为参数。任何的想法?
我知道 eclipse 会生成一个可运行的 jar 文件,其中包含提取并包含在 jar 文件中的所有库 .jar 文件。从命令提示符手动创建 jar 文件时,我如何执行类似的操作。我是否需要将所有 lib jar 解压缩到 classes 文件夹中?
目前我正在使用 -
jar cvfm example.jar manifest.txt -C Classes/ .
Run Code Online (Sandbox Code Playgroud)
这会创建一个包含所有类文件的 jar 文件,我需要将所有库 jar 放在与 example.jar 存在的同一文件夹中。
该项目的最终目标是获取 jar 并将其放入使用 tesseract 的目录中,并输出结果目录和输出 txt 文件。不过,我在使用超正方体时遇到了一些问题。我正在 Java 中使用 tess4j 和 Maven,我想将我的代码放入可执行 jar 中。该项目作为桌面应用程序运行良好,但每当我尝试使用java -jar fileName.jar(导出到 jar 后)运行时,它都会给我错误
Please make sure the TESSDATA_PREFIX environment variable is set to the parent directory of your "tessdata" directory
Failed loading language 'eng'
...
Run Code Online (Sandbox Code Playgroud)
我在网上查找,但无法真正找到如何为 jar 设置超正方体并获得正确的路径。现在我使用 maven 并在我的 pom 文件(tess4j -v 3.0)中有 Tesseract 依赖项,并且我的项目中有 tessdata。
我对 Maven 和 jar 文件相当陌生,以前从未使用过 tesseract,但据我从互联网上可以看出,我设置正确。
有谁知道如何使 tess4j 指向我项目中的 tessdata 目录并拥有动态路径,以便我可以在多台计算机和位置上移动使用它?
这就是我所说的 Tesseract
Tesseract instance = new Tesseract();
instance.setDatapath("src/main/resources");
String result = instance.doOCR(imageFile);
String fileName = …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Intellij's Cursive 中的 Leiningen 插件从我的基本 Clojure 项目中制作一个独立的 jar。
为了创建项目,我只是创建了 project.clj 文件,将其打开,然后 Cursive 提出将其作为项目导入。
项目.clj:
(defproject WaterTimer "1"
:description "A timer that reminds you to drink water"
:main tone-producer/main)
Run Code Online (Sandbox Code Playgroud)
音调producer.clj:
(ns tone-producer
(:require [general-helpers :as g])
(:import [javax.sound.midi MidiSystem
Synthesizer
MidiChannel])
(:gen-class))
(defn main [& args]
(println "Test!"))
Run Code Online (Sandbox Code Playgroud)
当我运行“uberjar”任务时,我得到以下输出:
警告:指定 :main 而不将其包含在 :aot 中。:main 的隐式 AOT 将在 Leiningen 3.0.0 中删除。如果您的 uberjar 只需要 AOT,请考虑将 :aot :all 添加到您的 :uberjar 配置文件中。警告:jar 中不存在指定的 Main-Class。它可能无法按预期执行。包含 main 方法的命名空间中可能缺少 gen-class 指令。创建 C:\Users\slomi\IdeaProjects\WaterTimer\target\WaterTimer-1.jar 创建 C:\Users\slomi\IdeaProjects\WaterTimer\target\WaterTimer-1-standalone.jar
我还尝试将main函数更改为具有默认名称,并从以下名称中省略名称 …
我有一个 Spring boot 应用程序,当从命令行运行时,它可以完美地记录到文件系统(通过logging.level/logging.file属性):
java -jar jarfilename.jar
Run Code Online (Sandbox Code Playgroud)
但是,当我将以下内容放入 pom.xml 中以创建可执行 JAR,然后尝试将其作为 Ubuntu 中的 init 脚本运行时,日志记录根本不会发生。
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
</plugins>
</build>
Run Code Online (Sandbox Code Playgroud)
以下是我在 Linux 上运行的命令:
ln -s /path/to/executablejar.jar /etc/init.d/myapp
chmod a+x /etc/myapp
/etc/init.d/myapp start
Run Code Online (Sandbox Code Playgroud)
我想这一定是日志记录配置的某种问题,没有考虑可执行 jar 的启动方式,但我很困惑并且希望得到任何指导。
这是我在 application.properties 中的日志记录设置:
logging.level.org.springframework=error
logging.level.com.myapp=debug
logging.level.org.hibernate=error
logging.file=/path/to/a.log
Run Code Online (Sandbox Code Playgroud) 我有一个 fat jar,用 Scala 编写,由 sbt 打包。我需要在 AWS EMR 的 Spark 集群中使用它。
如果我手动启动集群,将 jar 复制到 master 并使用这样的命令运行 Spark-submit 作业,它的功能就很好......
spark-submit --class org.company.platform.package.SparkSubmit --name platform ./platform-assembly-0.1.0.jar arg0 arg1 arg2
Run Code Online (Sandbox Code Playgroud)
但是...如果我尝试将其作为步骤添加到 EMR 集群,则会失败。stderr 的日志看起来像这样......
Exception in thread "main" java.lang.ClassNotFoundException: package.SparkSubmit
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.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 java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:278)
at org.apache.hadoop.util.RunJar.run(RunJar.java:214)
at org.apache.hadoop.util.RunJar.main(RunJar.java:136)
Run Code Online (Sandbox Code Playgroud)
我的 build.sbt 中的相关设置如下所示......
lazy val root = (project in file(".")).
settings(
name := "platform",
version := "0.1.0",
scalaVersion := "2.10.5", …Run Code Online (Sandbox Code Playgroud) 我正在尝试打包(maven 包)并运行(java -jar file.jar)一个包含 java 和 kotlin 代码的项目。我遵循了教程:https : //michaelrice.com/2016/08/hello-world-with-kotlin-and-maven/
kotlin source src/main/kotlin
java source src/main/java
我试图运行的文件位于 src/main/kotlin/THEFILE.kt
在成功打包尝试运行 jar 后,我收到一个错误
Error: Could not find or load main class THEFILEKt
这可能是什么原因以及如何解决?
提前致谢!!!
Pom.xml 包含必要的 kotlin 插件和依赖项:
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>${kotlin.version}</version>
</dependency>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<sourceDirs>
<source>src/main/java</source>
<source>src/main/kotlin</source>
</sourceDirs>
</configuration>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<jvmTarget>1.8</jvmTarget>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId> …Run Code Online (Sandbox Code Playgroud) JAR、Fat JAR 和可执行 JAR 之间有什么区别?它们是如何从命令行和 gradle.build 任务创建的(如果是 gradle 项目)?
除了上面提到的还有其他JAR吗?
我有以下项目结构。
没有 src/main 文件夹。
以上 src/test/java 文件夹包含一些 scala 类以及一些 java 类。当我使用以下插件构建 jar 文件时
请建议
如何生成解压所有依赖项的“测试”jar。或者在这种情况下如何使用 src/test/java 文件夹下的所有类以及测试类所需的所有依赖项生成 jar 文件
java executable-jar maven-jar-plugin maven-shade-plugin karate
executable-jar ×10
java ×8
jar ×6
maven ×3
amazon-emr ×1
clojure ×1
command-line ×1
init ×1
karate ×1
kotlin ×1
leiningen ×1
linux ×1
runnable ×1
scala ×1
spark-submit ×1
spring-boot ×1
tess4j ×1
tesseract ×1