Why same code builded with javac and gradle behave different? - java

switch (readChoice()) {
//options
}
private static int readChoice() {
String input;
int choice = -1;
try {
input = scanner.next();
choice = Integer.parseInt(input);
} catch (NumberFormatException e) {
logger.error("Error while trying to read choice from the console", e);
}
}
return choice;
}
When i build this code with javac, everything works fine. But when i try to run code builded with gradle, i get this
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1371)
at edu.java.core.cinema.Main.readChoice(Main.java:207)
at edu.java.core.cinema.Main.main(Main.java:42)
42 - there is switch, 207 - there is scanner.next().
Scanner tries to read choice, when there is nothing to read.
I just started to learn gradle, so maybe i did mistake with building.
I build and run project with gradle wrapper
$ gradle wrapper --gradle-version 3.4
$ ./gradlew build
$ ./gradlew run
UPDATE:
There is build.gradle file
apply plugin: 'idea'
apply plugin: 'java'
apply plugin: 'application'
mainClassName = 'edu.java.core.cinema.Main'
repositories {
mavenCentral()
}
jar {
baseName = 'lab2'
version = '0.1.0'
}
sourceSets.main.java.srcDirs = ["src"]
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.0'
compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.2'
compile group: 'ch.qos.logback', name: 'logback-core', version: '1.2.2'
compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.25'
}

In your gradle build file add
run {
standardInput = System.in
}
to redirect the "gradle input" to system.in. If you don't it's empty and will fail when trying to read input.

Related

How to choose a right javafx-base*.jar variant for MacOS?

I have the next build.gradle script:
plugins {
id 'application'
id 'org.openjfx.javafxplugin' version '0.0.8'
id "org.beryx.jlink" version "2.17.5"
}
group 'com.alcoholics'
version '1.0-SNAPSHOT'
application {
applicationDefaultJvmArgs = [
"--add-opens=javafx.base/com.sun.javafx.runtime=org.controlsfx.controls",
"--add-opens=javafx.base/com.sun.javafx.collections=org.controlsfx.controls",
"--add-opens=javafx.graphics/com.sun.javafx.css=org.controlsfx.controls",
"--add-opens=javafx.graphics/com.sun.javafx.scene=org.controlsfx.controls",
"--add-opens=javafx.graphics/com.sun.javafx.scene.traversal=org.controlsfx.controls",
"--add-opens=javafx.graphics/javafx.scene=org.controlsfx.controls",
"--add-opens=javafx.controls/com.sun.javafx.scene.control=org.controlsfx.controls",
"--add-opens=javafx.controls/com.sun.javafx.scene.control.behavior=org.controlsfx.controls",
"--add-opens=javafx.controls/javafx.scene.control.skin=org.controlsfx.controls"
]
}
javafx {
version = '14'
modules("javafx.controls", "javafx.fxml")
}
jlink {
options = ['--strip-debug', '--compress', '2', '--no-header-files', '--no-man-pages']
launcher {
name = 'vodka'
}
}
mainClassName = 'shitfaced/com.alcoholics.shitfaced.App'
repositories {
mavenCentral()
}
dependencies {
compile 'org.controlsfx:controlsfx:11.0.1'
compile 'org.webjars:font-awesome:5.13.0'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
When I run my app all is ok but on Tasks -> build -> jlink I get:
Error: Two versions of module javafx.base found in /Users/shitfaced/Projects/shitfaced/build/jlinkbase/jlinkjars (javafx-base-14-mac.jar and javafx-base-14-linux.jar)
How can I specify a variant for MacOS in my build.gradle or even to Gradle automatically chose a right library variant for current platform?

How to fix jvm options exports javafx 11 to com.jfoenix on gradle idea?

It's been 3 days that I look on the internet how to fix this on gradle
Caused by: java.lang.IllegalAccessError: class com.jfoenix.skins.JFXTabPaneSkin (in module com.jfoenix) cannot access class com.sun.javafx.scene.control.behavior.TabPaneBehavior (in module javafx.controls) because module javafx.controls does not export com.sun.javafx.scene.control.behavior to module com.jfoenix
On simple idea projects I was adding
--add-exports javafx.controls/com.sun.javafx.scene.control.behavior=com.jfoenix --add-exports javafx.controls/com.sun.javafx.scene.control=com.jfoenix --add-exports javafx.base/com.sun.javafx.binding=com.jfoenix --add-exports javafx.graphics/com.sun.javafx.stage=com.jfoenix --add-exports javafx.base/com.sun.javafx.event=com.jfoenix
I know, I have to add jvm options but how do I ?
I'm using gradle javafx 11.0.2, java 11, on intellij idea,
this is the build.gradle
plugins {
id 'java'
id 'application'
id 'org.openjfx.javafxplugin' version '0.0.8'
}
compileJava.options.encoding = 'UTF-8'
repositories {
mavenCentral()
}
dependencies {
// https://mvnrepository.com/artifact/com.jfoenix/jfoenix
compile group: 'com.jfoenix', name: 'jfoenix', version: '9.0.9'
compile group: 'de.jensd', name: 'fontawesomefx-fontawesome', version: '4.7.0-9.1.2'
// https://mvnrepository.com/artifact/de.jensd/fontawesomefx-commons
runtime group: 'de.jensd', name: 'fontawesomefx-commons', version: '9.1.2'
// https://mvnrepository.com/artifact/com.h2database/h2
compile group: 'com.h2database', name: 'h2', version: '1.4.199'
}
javafx {
version = "11.0.2"
modules = [ 'javafx.controls', 'javafx.fxml','javafx.graphics','javafx.base' ]
}
mainClassName = 'org.yanisboukir.agence.Main'
Thanks
If you are running a non modular project (you don't have module-info.java), to include the VM arguments in your run task, all you need to add to your build.gradle file is:
run {
jvmArgs = [
"--add-exports=javafx.controls/com.sun.javafx.scene.control.behavior=ALL-UNNAMED",
"--add-exports=javafx.controls/com.sun.javafx.scene.control=ALL-UNNAMED",
"--add-exports=javafx.base/com.sun.javafx.binding=ALL-UNNAMED",
"--add-exports=javafx.graphics/com.sun.javafx.stage=ALL-UNNAMED",
"--add-exports=javafx.controls/com.sun.javafx.scene.control.behavior=ALL-UNNAMED"
]
}
Note that in this case, you can't use --add-exports=...=com.jfoenix.
If you run a modular project, with a module descriptor like:
module hellofx {
requires javafx.controls;
requires javafx.fxml;
requires com.jfoenix;

opens org.openjfx to javafx.fxml;
exports org.openjfx;
}
now these are the VM arguments that you will have to include in your build file:
run {

 jvmArgs = [
"--add-exports=javafx.controls/com.sun.javafx.scene.control.behavior=com.jfoenix",
"--add-exports=javafx.controls/com.sun.javafx.scene.control=com.jfoenix",
"--add-exports=javafx.base/com.sun.javafx.binding=com.jfoenix",
"--add-exports=javafx.graphics/com.sun.javafx.stage=com.jfoenix",
"--add-exports=javafx.controls/com.sun.javafx.scene.control.behavior=com.jfoenix"
]

}
For newer versions of Gradle (6.x) the following worked for me:
compileJava {
options.compilerArgs.add('--add-exports=java.base/jdk.internal.vm.annotation=ALL-UNNAMED')
}
For Java test code:
compileTestJava {
options.compilerArgs.add('--add-exports=java.base/jdk.internal.vm.annotation=ALL-UNNAMED')
}
Compile-time following:
compileJava {
options.compilerArgs.add('--add-exports=javafx.controls/com.sun.javafx.scene.control.behavior=com.jfoenix')
options.compilerArgs.add('--add-exports=javafx.controls/com.sun.javafx.scene.control=com.jfoenix')
options.compilerArgs.add('--add-exports=--add-exports=javafx.base/com.sun.javafx.binding=com.jfoenix')
options.compilerArgs.add('--add-exports=javafx.graphics/com.sun.javafx.stage=com.jfoenix')
}
Run-time following
run {

 jvmArgs = [
"--add-exports=javafx.controls/com.sun.javafx.scene.control.behavior=com.jfoenix",
"--add-exports=javafx.controls/com.sun.javafx.scene.control=com.jfoenix",
"--add-exports=javafx.base/com.sun.javafx.binding=com.jfoenix",
"--add-exports=javafx.graphics/com.sun.javafx.stage=com.jfoenix"
]

}
We must set both of them. If we only set the first one idea can compile, however at the run time throw exception. Thanks #sakra #José Pereda
this didn't work for me in JDK 16:
run {
jvmArgs = [
"--add-exports=javafx.controls/com.sun.javafx.scene.control.behavior=com.jfoenix",
"--add-exports=javafx.controls/com.sun.javafx.scene.control=com.jfoenix",
"--add-exports=javafx.base/com.sun.javafx.binding=com.jfoenix",
"--add-exports=javafx.graphics/com.sun.javafx.stage=com.jfoenix",
"--add-exports=javafx.controls/com.sun.javafx.scene.control.behavior=com.jfoenix",
]
}
what worked instead is
run {
jvmArgs = [
"--add-opens=java.base/java.lang.reflect=com.jfoenix",
"--add-opens=java.base/java.lang.reflect=com.jfoenix",
"--add-opens=javafx.controls/com.sun.javafx.scene.control.behavior=com.jfoenix",
"--add-opens=javafx.base/com.sun.javafx.binding=com.jfoenix",
"--add-opens=javafx.graphics/com.sun.javafx.stage=com.jfoenix",
"--add-opens=javafx.base/com.sun.javafx.event=com.jfoenix",
]
}

Azure Pipeline Gradle build fails for Spring

Im trying to build and deploy a Spring API on Azure via a Yaml pipeline. But i get an error during the spring application gradle build saying.
Error: /home/vsts/work/1/s/gradlew failed with return code: 1
Could not find org.springframework.boot:spring-data-rest-hal-browser:
Expanding the Error,
Starting a Gradle Daemon (subsequent builds will be faster)
> Task :compileJava FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Could not resolve all files for configuration ':compileClasspath'.
> Could not find org.springframework.boot:spring-data-rest-hal-browser:.
Required by:
project :
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 39s
1 actionable task: 1 executed
Error: /home/vsts/work/1/s/gradlew failed with return code: 1
at ChildProcess.<anonymous> (/home/vsts/work/_tasks/Gradle_8d8eebd8-2b94-4c97-85af-839254cc6da4/2.151.0/node_modules/vsts-task-lib/toolrunner.js:639:25)
at emitTwo (events.js:106:13)
at ChildProcess.emit (events.js:191:7)
at maybeClose (internal/child_process.js:886:16)
at Process.ChildProcess._handle.onexit (internal/child_process.js:226:5)
##[error]Error: /home/vsts/work/1/s/gradlew failed with return code: 1
##[section]Finishing: Gradle
Things i have tried.
I have tried changing specifying the versions of spring-data-rest-hal-browser in my project from.
compile("org.springframework.boot:spring-data-rest-hal-browser")
to
compile("org.springframework.boot:spring-data-rest-hal-browser:2.4.0.RELEASE")
and finally
compile("org.springframework.boot:spring-data-rest-hal-browser:3.0.8.RELEASE")
But still the same Error results
This is my current build.gradle file in my repo
plugins {
id 'org.springframework.boot' version '2.1.3.RELEASE'
id 'java'
}
apply plugin: 'io.spring.dependency-management'
group = 'com.test.spring.api'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.11'
repositories {
mavenCentral()
}
configurations {
developmentOnly
runtimeClasspath {
extendsFrom developmentOnly
}
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-rest'
runtimeOnly 'mysql:mysql-connector-java'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
developmentOnly("org.springframework.boot:spring-boot-devtools")
compile("org.springframework.boot:spring-boot-starter-data-rest")
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile("org.springframework.boot:spring-data-rest-hal-browser")
compile("org.springframework.data:spring-data-rest-webmvc:3.1.5.RELEASE")
}
And this is my current azure-pipelines.yml file
# Maven
# Build your Java project and run tests with Apache Maven.
# Add steps that analyze code, save build artifacts, deploy, and more:
# https://learn.microsoft.com/azure/devops/pipelines/languages/java
trigger:
- master
pool:
vmImage: 'Ubuntu-16.04'
steps:
- task: Gradle#2
inputs:
workingDirectory: '$(system.defaultWorkingDirectory)'
gradleWrapperFile: 'gradlew'
gradleOptions: '-Xmx3072m'
javaHomeOption: 'JDKVersion'
jdkVersionOption: '1.8'
jdkArchitectureOption: 'x64'
publishJUnitResults: false
testResultsFiles: '**/TEST-*.xml'
tasks: 'build'
- task: CopyFiles#2
displayName: 'Copy Files to: Wireframe Directory on development server'
inputs:
SourceFolder: '$(Build.SourcesDirectory)'
TargetFolder: '$(Build.ArtifactStagingDirectory)'
- script:
cd '$(Build.ArtifactStagingDirectory)';
ls
- task: FtpUpload#1
displayName: 'FTP Upload: $(Build.ArtifactStagingDirectory)'
inputs:
credentialsOption: inputs
serverUrl: '[test server url]'
username: '[test username] '
password: '[test password] '
rootDirectory: '$(Build.ArtifactStagingDirectory)'
remoteDirectory: D:\home\site\wwwroot\webapps\ROOT\
preservePaths: true
I want to be able to automatically deploy my spring application to the server URL by pushing my code to it via the Azure devops pipeline which would build and deploy the spring application.
Thanks
So i resolved the issue by making some changes to the build.gradle file and build pipeline using help from the Microsoft Azure Repository for Intellij on Github: see here
My new build.gradle file looks like this:
plugins {
id 'org.jetbrains.intellij' version '0.4.1' apply false
id 'org.springframework.boot' version '2.1.3.RELEASE'
id 'java'
}
/**
*This is task for update Gradle wrapper version.
*/
task wrapper(type: Wrapper) {
gradleVersion = '4.7'
distributionUrl = "https://services.gradle.org/distributions/gradle-${gradleVersion}-all.zip"
}
apply plugin: 'io.spring.dependency-management'
group = 'com.test.spring.api'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
allprojects {
configurations {
developmentOnly
runtimeClasspath {
extendsFrom developmentOnly
}
}
repositories {
mavenCentral()
}
}
task unitTest(type: Test) {
exclude '**/ApplicationTests/**'
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-rest'
runtimeOnly 'mysql:mysql-connector-java'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
developmentOnly("org.springframework.boot:spring-boot-devtools")
compile("org.springframework.boot:spring-boot-starter-data-rest")
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile group: 'org.springframework.data', name: 'spring-data-rest-hal-browser', version: '3.0.8.RELEASE'
compile("org.springframework.data:spring-data-rest-webmvc:3.1.5.RELEASE")
}
tasks.withType(JavaCompile) {
options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
}
tasks.withType(FindBugs) {
ignoreFailures = true
reports {
html { enabled = true }
xml.enabled = !html.enabled
}
}
/**
* Preparing for release build
*/
task prepRelease() {
}
Also modified the azure-pipelines.yml file to include stacktrace and info to assist in debugging other gradle and build errors.
tasks: 'build --stacktrace --info'

ClassNotFound exception on ClassLoader.loadClass method when the class is in different module

I get ClassNotFound Exception in UserServiceApplication.kt file.
Below is my project structure.
Root Project
|
|---settings.gradle
|
|---common (library module) (Spring boot project)
| |
| |--build.gradle
| |--src/main/kotlin/com/simbalarry/common/util/StringValidator.kt
| |--src/main/kotlin/com/simbalarry/common/MainApp.kt
| |
|---user-manager (Spring boot project) (uses common)
| |--build.gradle
| |--src/main/kotlin/somepackage/UserServiceApplication.kt
Contents of all files
RootProject/settings.gradle
include ':common', ':user-manager'
rootProject.name = 'home-cuisine'
common/build.gradle
plugins {
id 'java-library'
id 'org.springframework.boot' version '2.1.3.RELEASE'
id 'org.jetbrains.kotlin.jvm' version '1.2.71'
id 'org.jetbrains.kotlin.plugin.spring' version '1.2.71'
}
apply plugin: 'io.spring.dependency-management'
group = 'com.simbalarry'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
ext['kotlin.version'] = '1.2.71'
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-reflect'
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
}
compileKotlin {
kotlinOptions {
freeCompilerArgs = ['-Xjsr305=strict']
jvmTarget = '1.8'
}
}
common/com/simbalarry/common/util/StringValidator.kt
package com.simbalarry.common.util
class StringValidator {
fun hello(): String {
return "Bye"
}
}
common/com/simbalarry/common/MainApp.kt
package com.simbalarry.common
class MainApp
fun main(args: Array<String>) {
}
user-manager/build.gradle
plugins {
id 'org.springframework.boot' version '2.1.3.RELEASE'
id 'org.jetbrains.kotlin.jvm' version '1.2.71'
id 'org.jetbrains.kotlin.plugin.spring' version '1.2.71'
}
apply plugin: 'io.spring.dependency-management'
group = 'com.simbalarry'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
test {
useJUnitPlatform()
}
repositories {
mavenCentral()
maven { url 'https://repo.spring.io/snapshot' }
maven { url 'https://repo.spring.io/milestone' }
}
dependencies {
implementation project(':common')
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.jetbrains.kotlin:kotlin-reflect'
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
}
compileKotlin {
kotlinOptions {
freeCompilerArgs = ['-Xjsr305=strict']
jvmTarget = '1.8'
}
}
user-manager/UserServiceApplication.kt
package com.simbalarry.usermanager
import com.simbalarry.common.util.StringValidator
class UserServiceApplication
fun main(args: Array<String>) {
println("Hieex")
val stringValidator = StringValidator()
}
When I do gradle bootRun on the user-manager package, I get the following exception
E:\Repos\HC7\user-manager>gradle bootRun
> Task :user-manager:bootRun FAILED
Hieex
Exception in thread "main" java.lang.NoClassDefFoundError: com/simbalarry/common/util/StringValidator
at com.simbalarry.usermanager.UserServiceApplicationKt.main(UserServiceApplication.kt:9)
Caused by: java.lang.ClassNotFoundException: com.simbalarry.common.util.StringValidator
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':user-manager:bootRun'.
> Process 'command 'C:\Program Files\Java\jdk1.8.0_181\bin\java.exe'' finished with non-zero exit value 1
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
Deprecated Gradle features were used in this build, making it incompatible with Gradle 6.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/5.2.1/userguide/command_line_interface.html#sec:command_line_warnings
BUILD FAILED in 4s
4 actionable tasks: 1 executed, 3 up-to-date
Can anyone guide me what is going wrong with this project. I had this problem in bigger project but I have pasted the narrowed down problem here.

NoSuchMethodError when using gradle + ProGuard

I'm trying to create ProGuard task for Gradle, but even example task for guard failed with java.lang.NoSuchMethodError: proguard.gradle.ProGuardTask.getLogging()Lorg/gradle/logging/LoggingManagerInternal, here is my gradle.build
buildscript {
repositories {
flatDir dirs: 'proguard'
}
dependencies {
classpath ':proguard:'
}
}
subprojects{
apply plugin: 'java'
apply plugin: 'application'
tasks.withType(Compile) {
options.encoding = 'UTF-8'
}
sourceCompability = 1.7
dependencies{
testCompile "junit:junit:4.11"
}
repositories{
mavenCentral()
}
}
project(':csl'){
version = "0.1"
dependencies{
runtime 'com.intellij:forms_rt:7.0.3'
runtime group: 'com.jgoodies', name: 'jgoodies-common', version: '1.7.0'
runtime group: 'com.jgoodies', name: 'jgoodies-forms', version: '1.7.2'
runtime group: 'com.esotericsoftware', name: 'kryonet', version: '2.22.0-RC1'
runtime group: 'com.esotericsoftware.kryo', name: 'kryo', version: '2.24.0'
runtime group: 'org.apache.directory.studio', name: 'org.apache.commons.io', version: '2.4'
}
task obfuscate(type: proguard.gradle.ProGuardTask) {
injars 'build/libs/csl.jar'
outjars 'build/libs/csl-obf.jar'
// libraryjars "${System.getProperty('java.home')}/lib/rt.jar"
// keep 'class org.gradle.** { *; }'
// printmapping 'csl.map'
// overloadaggressively
// repackageclasses ''
// allowaccessmodification
//
// keep 'public class mypackage.csl.Main { \
// public static void main(java.lang.String[]); \
// }'
}
}
Commented section not affect on result. In ./proguard there is only proguard.jar
Or maybe Intellij IDEA has integration with gradle and proguard? The only information I found is about Android Studio
I had the same problem after upgrading ProGuard from 4.1 to 5.1.
Upgrading gradle from 1.9 to 2.2.1 solved it for me.
Best regards
Christian Teister

Categories