How to enable Java assertions in Visual Studio Code - java

When running a Java program from the command line, assertions can be enabled with the -enableassertions option for the java command. Running this program would then (and only then) fail with an AssertionError:
public class App {
public static void main(String[] args) throws Exception {
foo(2);
}
private static void foo(int x) {
assert x > 5;
System.out.println(x);
}
}
How can this be done when running a Java program in Visual Studio Code with the Java Extension Pack?

Visual Studio Code manages launch configurations in the launch.json file in the project folder root.
The -enableassertions option can be added there with the vmArgs key like this:
{
"configurations": [
{
"type": "java",
"name": "My App",
"request": "launch",
"mainClass": "App",
"projectName": "my-app",
"vmArgs": "-enableassertions"
}
]
}

Related

Java Jolt with quarkus error java.lang.Boolean; no valid constructor

I am using:
Java 17
Quarkus 2.12.0.Final
Jolt 0.1.7
graalvm-ce-22.0.0
Native image
mvn clean package -DskipTests -Pnative -Dquarkus.native.container-build=true
Using this transformer:
{
"operation": "default",
"spec": {
"user": {
"enabled": true
}
}
}
When a run the application using non-native image, it works!!
When a run the application the application with native image, occurs this error:
at com.bazaarvoice.jolt.common.DeepCopy.simpleDeepCopy(DeepCopy.java:53)
at com.bazaarvoice.jolt.defaultr.MapKey.applyLiteralKeyToContainer(MapKey.java:57)
at com.bazaarvoice.jolt.defaultr.MapKey.applyChild(MapKey.java:44)
at com.bazaarvoice.jolt.defaultr.Key.applyChildren(Key.java:162)
at com.bazaarvoice.jolt.defaultr.MapKey.applyLiteralKeyToContainer(MapKey.java:67)
at com.bazaarvoice.jolt.defaultr.MapKey.applyChild(MapKey.java:44)
at com.bazaarvoice.jolt.defaultr.Key.applyChildren(Key.java:162)
at com.bazaarvoice.jolt.Defaultr.transform(Defaultr.java:242)
at com.bazaarvoice.jolt.Chainr$ContextualTransformAdapter.transform(Chainr.java:113)
at com.bazaarvoice.jolt.Chainr.doTransform(Chainr.java:240)
at com.bazaarvoice.jolt.Chainr.transform(Chainr.java:180)
at com.mypackage.myclass.Play.run(Play.java:20)
at io.quarkus.amazon.lambda.runtime.AmazonLambdaRecorder$1.processRequest(AmazonLambdaRecorder.java:170)
at io.quarkus.amazon.lambda.runtime.AbstractLambdaPollLoop$1.run(AbstractLambdaPollLoop.java:130)
at java.lang.Thread.run(Thread.java:833)
at com.oracle.svm.core.thread.PlatformThreads.threadStartRoutine(PlatformThreads.java:704)
at com.oracle.svm.core.posix.thread.PosixPlatformThreads.pthreadStartRoutine(PosixPlatformThreads.java:202)
caused by: java.io.InvalidClassException: java.lang.Boolean; no valid constructor
at java.io.ObjectStreamClass$ExceptionInfo.newInvalidClassException(ObjectStreamClass.java:174)
at java.io.ObjectStreamClass.checkDeserialize(ObjectStreamClass.java:921)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2236)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1744)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:514)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:472)
at com.bazaarvoice.jolt.common.DeepCopy.simpleDeepCopy(DeepCopy.java:50)
... 26 more
As java.lang.Boolean has no default constructor, I tried registerForReflection, but not works too.
EDIT
I have found I workaround:
{
"operation": "default",
"spec": {
"user": {
"enabled": "true"
}
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"user": {
"enabled": "=toBoolean"
}
}
}
EDIT 2
This also happen with other values like Integer, Long, because they do not have default constructor.
Those errors happen because how the copy is made:
Look here, but I am Newbie in native image, so I don't know why this kind of copy does not work.

How to use java runtime 11 in EMR cluster AWS

I'm creating a cluter in EMR aws and when spark runs my application I'm getting error below:
Exception in thread "main" java.lang.UnsupportedClassVersionError:
com/example/demodriver/MyClassFromJAR has been compiled by a more recent version of the Java Runtime (class file version 55.0),
this version of the Java Runtime only recognizes class file versions up to 52.0
I'm using releaseLabel emr-6.5.0 on clusters and My driver jar is built in java11
How run java11 application in EMR? Or this error is about anything else?
In the most recent versions of EMR, java 11 is installed. To enable it, you can provide the following configuration.
[
{
"Classification": "spark-env",
"Configurations": [
{
"Classification": "export",
"Properties": {
"JAVA_HOME": "/usr/lib/jvm/java-11-amazon-corretto.x86_64"
}
}
]
},
{
"Classification": "spark-defaults",
"Properties": {
"spark.executorEnv.JAVA_HOME": "/usr/lib/jvm/java-11-amazon-corretto.x86_64"
}
}
]
This does not seem to be documented.
The defaultJavaOptions and extraJavaOptions might contain incompatible options for java 11 which you still might need to adapt/update.
Here is the full configuration including the necessary JVM options:
[
{
"Classification": "spark-env",
"Configurations": [
{
"Classification": "export",
"Properties": {
"JAVA_HOME": "/usr/lib/jvm/java-11-amazon-corretto.x86_64"
}
}
]
},
{
"Classification": "spark-defaults",
"Properties": {
"spark.executorEnv.JAVA_HOME": "/usr/lib/jvm/java-11-amazon-corretto.x86_64",
"spark.driver.defaultJavaOptions": "-XX:OnOutOfMemoryError='kill -9 %p' -XX:MaxHeapFreeRatio=70",
"spark.executor.defaultJavaOptions": "-verbose:gc -Xlog:gc*::time -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:OnOutOfMemoryError='kill -9 %p' -XX:MaxHeapFreeRatio=70 -XX:+IgnoreUnrecognizedVMOptions"
}
}
]
For running Spark (3.3.0) on EMR 6 (6.9.0) I had to provide the following (note the additional "hadoop-env" and the empty "Properties" elements):
[
{
"Classification": "hadoop-env",
"Configurations": [
{
"Classification": "export",
"Configurations": [],
"Properties": {
"JAVA_HOME": "/usr/lib/jvm/java-11-amazon-corretto.x86_64"
}
}
],
"Properties": {}
},
{
"Classification": "spark-env",
"Configurations": [
{
"Classification": "export",
"Configurations": [],
"Properties": {
"JAVA_HOME": "/usr/lib/jvm/java-11-amazon-corretto.x86_64"
}
}
],
"Properties": {}
},
{
"Classification": "spark-defaults",
"Properties": {
"spark.executorEnv.JAVA_HOME": "/usr/lib/jvm/java-11-amazon-corretto.x86_64"
}
}
]

How to debug a spring boot app in VS Code with command line arguments?

I've tried to create a VS Code launch configuration which passes command line arguments to a Spring Boot app.
#SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
for (String arg : args) {
System.out.println(arg);
}
SpringApplication.run(DemoApplication.class, args);
}
}
"configurations": [
{
"type": "java",
"name": "CodeLens (Launch) - DemoApplication",
"request": "launch",
"mainClass": "com.jackdry.processors.kafkalz4_compress.DemoApplication",
"projectName": "kafka-lz4_compress",
"vmArgs": "-Dspring-boot.run.arguments=-color=red"
}
]
But when I press "Debug" in VSCode, -color=red isn't being printed. Can anybody help?
If useful, the command which "Debug" actually invokes is:
cd /home/jack/jackdry/processors/kafka-lz4_compress ; /usr/lib/jvm/java-11-openjdk-amd64/bin/java -agentlib:jdwp=transport=dt_socket,server=n,suspend=y,address=localhost:38675 -Dspring-boot.run.arguments=-color=red -Dfile.encoding=UTF-8 #/tmp/cp_3noz6bed0u43t5tl3jj9cqoel.argfile com.jackdry.processors.kafkalz4_compress.DemoApplication

How can I execute a java program without a main method with VSCode?

I want to debug a java program without a main method using selenide tools. I can run the program from command line without any problem but when I try to debug it with VSCode, it outputs
The file 'Weather.java' is not executable. Please select a main class you want to run.
Is there any way to debug my program without using a main method?"
Weather.java
package com.example.app;
import static com.codeborne.selenide.Selenide.*;
// import static com.codeborne.selenide.Condition.*;
// import static com.codeborne.selenide.Selectors.*;
import com.codeborne.selenide.Configuration;
import com.codeborne.selenide.WebDriverRunner;
// import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvFileSource;
class Weather {
#ParameterizedTest
#CsvFileSource(resources = "Weather.csv", numLinesToSkip = 1)
void openWeather(String ward) {
String area = System.getProperty("area");
Configuration.browser = WebDriverRunner.CHROME;
// Configuration.headless = true;
// Googleトップページ
open("https://www.google.co.jp/");
// "天気"を検索
$("input[type=text]").val("天気").pressEnter();
// Youtube検索ページへ飛ぶ
$x("//a[#href='https://weather.yahoo.co.jp/weather/jp/13/4410/13120.html']").click();
$("#searchText").setValue(area);
$("#yjw_button_search").click();
$x("//a[text()= '" + ward + "']").click();
}
}
launch.json
{
"version": "0.2.0",
"configurations": [
{
"type": "java",
"name": "Debug (Launch) - Current File",
"request": "launch",
"mainClass": "${file}",
"preLaunchTask": "Run Test"
}
]
}
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "Run Test",
"type": "shell",
"command": "mvn test -Dtest=Weather -Darea=\"東京\"",
"problemMatcher": []
}
]
}
Current versions are as follows:
selenide:5.5.2
VSCode:1.41.0
junit:5.3.2
Apache Maven:3.6.0
Run tests with the Java extensions for Visual Studio Code was to simply click on the Explorer (file) icon, then expand the Test Explorer option, explore down to whatever test(s) you want to run, right-click, and then choose your testing options. There is no need to use the launch.json file to do this.

how to create executable jar file with cucumber tests?

My service create an executable jar by gradle. When i create and run my jar (java -jar file.jar) i recive error:
no main manifest attribute, in "file.jar"
because i don't have main_class.
I created main method:
public static void main(final String[] args) throws Throwable {
String[] arguments = {"--plugin", "html:build/reports/cucumber", "src/test/resources/features", "--glue", "src/test/java/steps"};
cucumber.api.cli.Main.main(arguments);
}
and My program founds the features but doesn't found glue code.
Could someone help me with this problem? Thank you in advance.
The value for the glue option should be a java classpath. And the feature files should be the last option.
{"--plugin", "html:build/reports/cucumber", "--glue", "steps", "src/test/resources/features"}
The below code worked for me to execute the cucumber tests from runnable jar with test frame work as TestNG.
Executing jar:
java -jar ProductsAutomation-0.0.1-SNAPSHOT-jar-with-dependencies.jar
import io.cucumber.core.cli.Main;
public static void main(String args[]) throws Throwable {
try {
Main.main(new String[] {
"-g","com.sadakar.cucumber.common",
"-g","com.sadakar.cucumber.runner",
"classpath:features",
"-t","#SmokeTest",
"-p", "pretty",
"-p", "json:target/cucumber-reports/cucumber.json",
"-p", "html:target/cucumber-reports/cucumberreport.html",
"-m"
}
);
} catch (Exception e) {
e.printStackTrace();
System.out.println("Main method exception : " + e);
}
}

Categories

Resources