Error: Unable to access jarfile build/libs/gs-spring-boot-0.1.0.jar? - gradle

I follow the instructions in https://spring.io/guides/gs/spring-boot/#scratch, but when it says to run:
./gradlew build && java -jar build/libs/gs-spring-boot-0.1.0.jar
the build fails with the above error.
There is message before the failure that says:
Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.
See https://docs.gradle.org/4.8.1/userguide/command_line_interface.html#sec:command_line_warnings
but everyone online says that's just a warning.
The build doesn't appear to create or download build/libs/gs-spring-boot-0.1.0.jar.
Currently completely blocked on first attempt to use Gradle.

I just had this problem.
The tutorial is in error in what you need to run. It should be
$ gradlew build && java -jar build/libs/gs-rest-service-0.1.0.jar
I think that they updated the code, but forgot to update the tutorial.

I had the same issue when build a simple project with Maven on Intellij IDEA. (Ubuntu 18.04.2).
Just typed terminal (in project directory):
$ sudo mvn package
$ java -jar ./target/(your-project-name)-(<version> at pom.xml).jar
For example my project name is hello-world-spring and version name in pom.xml is <version>0.0.1-SNAPSHOT</version>, I have to type:
$ sudo mvn package
$ java -jar ./target/hello-world-spring-0.0.1-SNAPSHOT.jar
Maybe this method can work for gradle as well.

Please check the path of the jar file build/libs/gs-spring-boot-0.1.0.jar. For your case, the jar might be in a different folder. If your code is in a module in the main project, then the jar will be in the build folder of the module.

If you git clone the repo, then the tutorial works. If you "To start from scratch, move on to Build with Gradle.", then the tutorial doesn't work. There are missing setup steps.

I got the same issue and I changed the command to java -jar target/rest-service-0.0.1-SNAPSHOT.jar (I checked the .jar file in target folder and found that the file name was incorrect).

Parent folder of my project was having spaces in it's name, i changed it to the underscore and it worked.

Looked at the command line as it was in the official guide:
./gradlew clean build && java -jar build/libs/gs-actuator-service-0.1.0.jar
First, the above command line has two parts:
(1) ./gradlew clean build //Use gradle wrapper to build
(2) java -jar build/libs/gs-actuator-service-0.1.0.jar //To run an application packaged as a JAR file
Now, one might run into issues with one part or both parts. Separating them and running just on thing at a time helped troubleshoot.
(1) didn't work for my Windows, I did the following instead and that built the application successfully.
.\gradlew.bat clean build
Now moving to (2) java -jar build/libs/gs-actuator-service-0.1.0.jar
It literally means that "Run a jar file that is called gs-actuator-service-0.1.0.jar under this directory/path: build/libs/" Again, for Windows, this translates to build\libs\ , and there's one more thing that may catch you: The jar file name can be slightly different depending on how it was actually named by the configuration in initial/setting.gradle:
rootProject.name = 'actuator-service'
Note that the official guide changed it from 'gs-actuator-service' to 'actuator-service' in their sample code but hasn't updated the tutorial accordingly. But now you know where the jar file name comes from, that doesn't matter anymore, and you have the choice to rename it however you want.
Having all the factors adjusted, below is what eventually worked in my case:
java -jar build\libs\actuator-service-0.0.1-SNAPSHOT.jar
or
java -jar C:\MyWorkspace\Spring\gs-actuator-service\initial\build\libs\actuator-service-0.0.1-SNAPSHOT.jar //with fully qualified path
If you are curious where does "-0.0.1-SNAPSHOT" come from, here it is:
in build.gradle
version = '0.0.1-SNAPSHOT'
Again, you have the choice to modify it however you want. For example, if I changed it to 0.0.2-SNAPSHOT, the command line should be adjusted accordingly
java -jar build\libs\actuator-service-0.0.2-SNAPSHOT.jar
Reference: https://docs.oracle.com/javase/tutorial/deployment/jar/basicsindex.html

Because you are trying to execute .jar file that doesn't exist. After building the project go to ./build/libs and check the name of freshly built .jar file and then in your project directory run:
./gradlew build && java -jar build/libs/name-of-your-jar-file.jar
or you can set version property to empty string in your build.gradle file
version = ''
after that:
./gradlew build && java -jar build/libs/your-project-name.jar

For Windows, these commands solved the problem: "Error: Unable to access jarfile springboot.jar":
cd target
java -jar springboot-0.0.1-SNAPSHOT.jar

run ./mvnw package
Now a folder named target is created and you can see a jar file inside it.
then execute java -jar target/<jarfilename>

Related

Unable to execute a JavaFX application bundled without JRE

I'm having some troubles trying to deploy a JavaFX application. In order to simplify my problem I've tried to do the same with a "Hello word" application and the problem is the same.
I'm currently using IntelliJ IDEA and Gradle.
My build.gradle file is this:
apply plugin: 'java'
apply from: "http://dl.bintray.com/content/shemnon/javafx-gradle/8.1.1/javafx.plugin"
repositories {
mavenCentral()
}
javafx {
mainClass 'Main'
}
That build.gradle file works. The problem is that it embeds the JRE into the bundle so the file size is about 175 MB. That's too much for a simple "Hello World" app, don't you think?
So, I want to bundle this simple app without the JRE (yes, I know that I should distribute my app with the JRE bundled so it doesn't relay on uses system but I'm going to distribute both versions: with and without JRE bundled). In order to do this I add a single line to the build.gradle file (as explained in this link:
...
javafx {
mainClass 'Main'
javaRuntime '<NO RUNTIME>'
}
But no bundles are generated when gradle jfxDeploy. In fact, running gradle jfxDeploy -i show some interesting info:
Java runtime to be bundled: none, bundle will rely on locally installed runtimes
...
Skipping Mac Application Image because of configuration error The file for the Runtime/JRE directory does not exist.
Advice to Fix: Point the runtime parameter to a directory that containes the JRE.
Skipping DMG Installer because of configuration error The file for the Runtime/JRE directory does not exist.
Advice to Fix: Point the runtime parameter to a directory that containes the JRE.
Skipping PKG Installer because of configuration error The file for the Runtime/JRE directory does not exist.
Advice to Fix: Point the runtime parameter to a directory that containes the JRE.
Skipping Mac App Store Ready Bundler because of configuration error The file for the Runtime/JRE directory does not exist.
Advice to Fix: Point the runtime parameter to a directory that containes the JRE.
Ok, so maybe the plugin has some bugs. I try to generate the bundle with javapackager. I go to project folder and run the following:
javapackager -deploy -native image -srcfiles build/libs/ -outdir build/distributions -outfile Sample -appclass Main
The output is OK. The bundle is correctly generated with the JRE embedded. Now I try to generate a bundle without the JRE with this:
javapackager -deploy -native image -srcfiles build/libs/ -outdir build/distributions -outfile Sample -appclass Main -Bruntime=
(It's the same command appending -Bruntime= as explained in this link).
The bundle is generated. Now its size is about 500 KB. But when I try to run it nothing happens. Running it in a terminal gives the following (simplified) output:
$ Main.app/Contents/MacOS/Main
Failed to find library.:
Main:Failed to locate JNI_CreateJavaVM
Main:Failed to launch JVM
It seems that the bundle is not capable to start the local JVM. The jar is correctly generated and added to the bundle. Running it with java -jar runs the app but I don't know why it doesn't work when running the bundle
FYI, I'm running java 1.8.0_74, javac 1.8.0_74 and javapackager 8.0 in an OS X 10.11.2
The javafx.plugin from shemnon isn't developed nor maintained anymore, for that reason I've created the javafx-gradle-plugin.
The problem comes with the internal changes of the .cfg-file-format, they use INI-files now, but that is flawed in term of RUNTIME-configuration.
Official JDK-bug-tickets reported by me:
(jdk 9) https://bugs.openjdk.java.net/browse/JDK-8143314
(jdk 8) https://bugs.openjdk.java.net/browse/JDK-8143934
It should be enough to set some bundler-argument launcher-cfg-format to cfg within your build (or use the javafx-gradle-plugin, which includes that workaround automatically).
Disclaimer: I'm the maintainer of the javafx-maven-plugin and the creator and maintainer of the javafx-gradle-plugin.
UPDATE this got fixed and made available with JDK 8u92

Apache Maven is not configured properly into System vairable

I have latest version of Apache Maven 3.3.9 residing in my drive downloaded from here. I need it for configuring Appium tool to perform automated testing of Android applications. I have Maven plugin already configured with my Eclipse IDE. I went through few articles and got to know that I have to configure Maven into system variables as well. I set system variables, path for it as below.
M2HOME = C:\Program Files (x86)\Apache\apache-maven-3.3.9
M2 = %M2HOME%\bin
path = C:\Program Files\Java\jdk1.8.0_31\bin;%M2%
I tried running mvn , mvn -version from cmd prompt to check successful configuration of Maven but it gave me nothing. I tried navigating to the bin folder and re run the command but still the result is same. I even changed my path variable to have absolute path of Apache Maven i.e path = C:\Program Files (x86)\Apache\apache-maven-3.3.9\bin
To be further sure I copied the Apache Maven to a different folder just because it had spaces in between Program Files and Program Files (x86) but still the command prompt is as below:
I had followed above steps referring various articles in google. I'm not getting if I have committed any unseen mistake.
P.S: I have even tried above commands with cmd running as an administrator
Try mvn --version (with two dashes). mvn -v should also work. If Maven is not being executed at all, I suggest you try mvn.bat.
Some Command Prompt configurations will refuse to run .bat-files without the extension unless you explicitly tell them to.
It's probably a good idea to run where mvn in your Command Prompt as well. You should get this:
<YOUR_MAVEN_HOME>\bin\mvn
<YOUR_MAVEN_HOME>\bin\mvn.bat
<YOUR_MAVEN_HOME>\bin\mvn.cmd
if everything it set up correctly.
You should define M2_HOME variable. (Not M2HOME). Make sure it looks like this:
You said that you copied your maven to the location without spaces. That's a good idea. But it looks like your Path system variable points to the old location of maven. Make sure to change it. (What is the current location of maven?)
BTW mvn -version works as well as mvn --version

Having issues running java project through command line

I am completely new to using maven. I have created a maven project and exported it to eclipse. Maven automatically created the src/test/java and src/main/java.
I created a java script and successfully ran it in eclipse. But when I try running it through command line, I got an error that says:
cannot find or load main class.
When I checked my project directory, there were two classfile paths: classes and test-classes. The script I was running is the one in 'test-classes' but it is not the main class. But the path to the main class executes successfully but that is not where my script is located.
The command I was using for the main classfile is:
java -cp target/Test-1.0-SNAPSHOT.jar com.mycompany.App.
The command for the test classfile is :
java -cp target/Test-1.0-SNAPSHOT.jar com.mycompany.AppTest.
This second command gives me the error I mentioned.
Please how do I get around this issue?
By convention, everything in src/test/java is supposed to be for testing only. That is, you should place only your test classes in this directory. Maven will run them before building the final JAR, but it will NOT include them in it.
If you absolutely need a JAR with your test classes, have a look at How to create a jar containing test classes.

problems running state machine examples

Congratulations on the spring state machine, I found it yesterday and have been trying it out, specifically the turnstile example running in STS. I found it very easy and intuitive to build a FSM.
Because spring shell doesn't work well in STS I tracked down the instructions to run the examples from the command line in the reference doc,
"java -jar
spring-statemachine-samples-turnstile-1.0.0.BUILD-SNAPSHOT.jar"
,
but running it got an error
"no main manifest attribute, in spring-statemachine-samples-turnstile-1.0.0.BUILD-SNAPSHOT.jar".
Although not even a novice in using gradle, I tried fixing this by adding this line to build.gradle in the jar section
"manifest.attributes['Main-Class'] = 'demo.turnstile.Application'"
(which doesn't handle the various sub-projects I know) but got this error
"NoClassDefFoundError: org/springframework/shell/Bootstrap".
If it is possible to run the samples from gradle, could you include them in the reference document? I tried running the samples using
gradle run
but it there was no interaction with the shell scripts.
Samples are designed to be run as executable jar and with shell so that you can interact without a need to recompile with every change. Your error indicates that you didn't build that sample jar as mentioned in docs.
./gradlew clean build -x test
This will automatically use spring boot plugin which will add the necessary jar manifest headers to jar meta info to make it a true executable jar. Essentially every every sample is a spring boot app.
Building SM sample projects in Windows Environment:
Open Command prompt (windows key + r -->cmd-->Enter), Change directory to project root folder spring-statemachine-master (Inside the Extracted folder).
Run gradlew install to get all spring dependencies copied to local machine.
Run gradlew clean build -x test to get the spring shell jars built. Courtesy Janne
These steps should ideally get all .jar built, look into \build\libs folder of respective sample project for jar files.
Run the like any other java jar file java -jar [jar-file-name.jar] (make sure to be change directory to jar file directory location).
One more thing where I was stuck was, How to give events to SM:
It's like this sm event EVENT_NAME_AS_DEFINED_IN_CLASS. Ref
E.g.: sm event RINSE --> to washer project

How run mahout in action example ReutersToSparseVectors?

I want run "ReutersToSparseVectors.java". I can compile and created JAR file without problem.
I compiled this file by below command:
javac -classpath hadoop-core-0.20.205.0.jar:lucene-core-3.6.0.jar:mahout-core-0.7.jar:mahout-math-0.7.jar ReutersToSparseVectors.java
created JAR file with below command:
jar cvf ReutersToSparseVectors.jar ReutersToSparseVectors.class
When I write java -jar ReutersToSparseVectors.jar to run, give me below error:
Failed to load Main-Class manifest attribute from
ReutersToSparseVectors.jar
Do you can help me to solve this problem?
IF this example can run with hadoop, please me that how i can run this with hadoop.
instead of using -jar option, then it's better to to run:
java -cp mahout-core.jar:... mia.clustering.ch09.ReutersToSparseVectors
or you can use mvn exec:java command, as described in README for examples...
mvn exec:java -Dexec.mainClass="mia.clustering.ch09.ReutersToSparseVectors"
Or you can run this file directly from your IDE (assuming, that you correctly imported Maven project).
P.S. your command isn't working, because to run with -jar switch, the .jar file should have special entry in manifest that describes that class should be started by default...
P.P.S. It's better to use book's examples with Mahout 0.7, as they were tested for it. You can use it with version 0.7 if you need, by then you need to take source code from mahout-0.7 branch of repository with examples (link is above)

Resources