jshell classpath in Windows - java-9

I have a list of JARS in a directory and I need to add then to JShell classpath. I was trying to avoid typing each individual JAR file name and using wildcard "*" by CLASSPATH environment variable. But in Windows 10, apparently, jshell is not taking the classpath.
I first tried setting the classpath in command prompt.
SET CLASSPATH=lib/*
Did not work when I started jshell. When I executed /env command, gave a blank response.
Next, I tried jshell --class-path=%CLASSPATH%. This type, when I typed /env command, it listed out all JARS in classpath. But still I could not import the classes in the JAR.
Please let me know what is the issue. Thanks in advance.

It was an issue with intellij cache. There was no issue with JShell classpath.

Related

Installing Log4J 2 on Windows 10

I am trying to install log4j (2) on Windows 10. I downloaded and extracted log4j from https://logging.apache.org/log4j/1.2/download.html
However I don't know what do add to the path variable or how to call log4j on windows 10.
The latest steps on Linux for log4j where:
$ export CLASSPATH=$CLASSPATH:/usr/local/apache-log4j-1.2.15/log4j-1.2.15.jar
$ export PATH=$PATH:/usr/local/apache-log4j-1.2.15/
The Problem here is, as far as I know, Windows has no classpath and there is no log4j.jar file just log4j-core and log4j-api (and similar) jars.
If someone could help me set this up correctly I'd greatly appreciate this.
Log4j does not require installation. It is a set of libraries. You should not specify the CLASSPATH as a system-wide environment variable (however it is acceptable). If you want your application make use of log4j you should propagate the class path to log4j libraries as a parameter to java.exe. If you need those libs on development phase you should either specify the path in your IDE project settings (the way how to do this is specific for each particular IDE), or, if you use standard maven project type, specify the appropriate dependency in pom file so that IDE takes care of proper classpath configuration.
Please also refer to this post helping you to get started with log4j.

Spring boot running a fully executable JAR and specify -D properties

The Spring Boot Maven and Gradle plugins can now generate full executable archives for Linux/Unix operating systems.Running a fully executable JAR is as easy as typing:
$ ./myapp.jar
My question is in this case how to set -D properties, e.g.
-Dspring.profiles.active=test
In addition, if server does not install jdk , could this fully executable jar still run?
There are two ways to configure properties like that:
1:
By specifying them in a separate configuration file. Spring Boot will look for a file named like JARfilename.conf which should be stored in the same folder like the JAR file. There you can add the environment variable JAVA_OPTS:
JAVA_OPTS="-Dpropertykey=propvalue"
2:
Or you can just specify the value for the environment variable in the shell before you execute the application:
JAVA_OPTS="-Dpropertykey=propvalue" ./myapp.jar
Have a look at the documentation for the complete list of available variables: http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#deployment-service
Regarding your second question: To execute a JAR, you don't need a JDK, a JRE is sufficient (but you need at least that, if you don't have any java installed on the server, the application won't run).
By default SpringApplication will convert any command line option arguments (starting with ‘--’, e.g. --server.port=9000) to a property and add it to the Spring Environment. As mentioned above, command line properties always take precedence over other property sources.
e.g.
$ java -jar myapp.jar --spring.application.json='{"foo":"bar"}'
please see http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/

What is Maven doing with my testclasspath

I'm having issues with a test, which when executed in maven fails to initialize log4j, although a valid log4j.properties is in src/test/resources and therefore should end up on the classpath of the test. But it doesn't, i.e. log4j prints only
log4j:WARN No appenders could be found for logger (org.springframework.test.context.junit4.SpringJUnit4ClassRunner).
log4j:WARN Please initialize the log4j system properly.
In order to debug the problem I printed the classpath from the test itself, using the code here
But instead of a lengthy list of jars and paths I just get
/<projectpath>/target/surefire/surefirebooter6226797341642271676.jar
So my questions are:
WTF is maven doing with the classpath?
Why doesn't my log4j.properties end up on the classpath?
How do I debug this?
Note: In Eclipse I can run the test just fine and everything works as expected.
Another note: the maven project is a multimodule project and I'm only executing a single test from a single submodule, with a commandline like this:
mvn -U -Dtest=de.company.project.SomeTest clean test
Have a good look at the maven-surefire-plugin. By default it creates a jar stuffed with your entire classpath. This is controlled by the useManifestOnlyJar option. This works around the problem of Windows having a classpath limit of 1024 (quoting off the top of my head). Under Linux you wouldn't really feel this pain much as the limit is much higher.
If you are forking the maven-surefire-plugin, it will use a different classpath than the one you're running Maven (and the compilation).
Debugging this kind of crappy situation can be done as follows:
In one of your tests add a loop that lists all the environment variables along with the java system properties.
Debug the tests:
mvn -Dmaven.surefire.debug="-Xdebug \
-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=9001 \
-Xnoagent" \
test
I found the answer to question 1.
Maven creates the jar with the weird name on the fly and just puts a MANIFEST.MF file in there. That file contains the classpath, and the main class to be started.
This also answers to some extend question 3.
You can copy that jar file somewhere else, while maven is running, so it does not delete it once it is finished. Then you can examine it as long as you want. Turns out my log4.properties is on the classpath (the target directories for the testclasses is there and the properties files is in that directory ....)
Leaves me with question 2.
It turned out somewhere in the forest of pom.xmls the system property log4j.configuration was set to a rather useless value. Setting that value back to the propervalue as described here solved my immediate problem.
Now I just have to find the broken spot in our poms, but that's a story for a different day.

How to tell maven to get MAVEN_OPTS from specific file?

mvn command, among others, have following options:
-f,--file <arg> Force the use of an alternate POM file. (This is for pointing file instead of default pom.xml file.)
-gs,--global-settings <arg> Alternate path for the global settings file. (This one is for pointing the settings.xml file, which is by default in .m2 directory.)
Still there is yet one config file uncovered by these options - .mavenrc
So, my question is - Is there a way to tell maven from which file it should get MAVEN_OPTS?
MAVEN_OPTS is a environment variable from the OS. You can set it anyway you want before launching maven.
In bash (linux):
export MAVEN_OPTS=...
On windows:
set MAVEN_OPTS=...
I think you could even edit the 'mvn' of 'mvn.bat' shell script to get different variables.
Starting with Maven 3.3.1, you can put these settings into the .mvn/maven.config file in your project repository.
References:
JVM and Command Line Options
MNG-5767

java programs not running due to seting classpath

i wanted to practice developing database programs in java so i set the classpath to E:\software\installed\java\jre\lib by declaring a new environmental variable classpath and gave value as E:\software\installed\java\jre\lib but now i am unable to run any program(not even non-database) i get an error
Error: Could not find or load main class MysqlConnect
but when i delete the classpath variable i am able to run non-database programs. what is the possible problem and please let me know the solution. i have set path to E:\software\installed\java\bin
nothing is helping i tried using class path switch in java which is not helping (java -cp C:\Program Files\MySQL\MySQL Server 5.5 MysqlConnect) nor do setting path is helping (set CLASSPATH=%CLASSPATH%:C:\Program Files\MySQL\MySQL Server 5.5:C:\Program Files\MySQL\MySQL Server 5.5\mysql-connector-java-5.1.20-bin.jar) all are giving the error
Error: Could not find or load main class MysqlConnect
You shouldn't be defining a classpath for your programs using system variables. Standard way is to use the command line -cp or -classpath option. Take a look at what java prints out if you run it with no arguments.
You will find this mentioned in this tutorial. It says:
The preferred way to specify the class path is by using the -cp command line switch. This allows the CLASSPATH to be set individually for each application without affecting other applications. Setting the CLASSPATH can be tricky and should be performed with care
Also, see how the default CLASSPATH environmetn variable has a . in it. If you still decide to add your classes in there, keep the . for other citizens and add your classes after a ;, don't just overwrite the whole value.
Here's a jdbc tutorial for beginners that could help you find your answer. If you skip to step 14 it gives an example of how the classpath is used. It is used as the first respondent is saying, by command line.
Hopefully this will help you in your particular situation. This tutorial is not database-specific so it will work with any type of database you are using.
ok atlast i got the solution it should be typed as
C:\Users\sarad mohanan\Desktop\rose>java -cp .;"c:\Program Files\MySQL\MySQL Server 5.5\mysql-connector-java-5.1.20-bin.jar" MysqlConnect
MySQL Connect Example.
Connected to the database
Disconnected from database
we have to add .; before the original path . holds default classpath. pravel veller had said it previously but i didn't understand it then
I would like to propose using a good tool like Eclipse. You can test the CLASSPATH indirectly and your environment by adding or removing jar files and libraries.
Obviously, the libraries in your default CLASSPATH is different than the one in your E drive. You may compare the files by knowing your CLASSPATH. In Windows, do "echo %CLASSPATH%".
Eclipse can help you by experimenting without rebooting or any annoying steps. And this stuff is not easy.
Crete o set environment variable if you use windows JAVA_HOME=E:\software\installed\java, and after append the Java bin directory %JAVA_HOME%\bin. Note that paths are separated from each other with semicolons (;) I hope help you. That's work for me !!!. I don't understand. What happen?.

Resources