VS Code doesn't see updated dependency in local repository - java

In a Java project I have a dependency on my own library. I have installed this library in the local repository by running mvn install in the root of the library's directory.
When I updated the library and re-installed it, VS Code couldn't see a newly-added method (everything compiles fine from the command-line).
I've tried Java: Force Java Compilation (full) and that didn't work.
In pom.xml I right-clicked on my library and chose Update Project and that didn't help either.
If it makes any difference, I am running on WSL2 (Ubuntu-20.04) on Windows 10.
Is there any solution short of restarting VS Code (which is a rather cumbersome workaround)?

Executing the Java: Clean Java Language Server Workspace command from the Command Palette (Ctrl+Shift+P) should work.
If you added new dependencies or jars, java language server will build and compile them automatically.
In some rare cases, you may need to execute the above command to let the language server rebuild your dependencies.
Reference: Library Configuration

Related

Change target JRE (JAVA_HOME & PATH) when running maven goals in VS Code

The Windows 10 system environment variables JAVA_HOME and PATH are pointing to JDK 18 which is needed to launch VS Code and needed by other applications on the laptop.
I configured Maven Project in VS Code with JRE/Java Runtime pointing to JDK 1.8 which is required for the project to compile and build correctly. All is working fine.
When I run Maven Goals such as mvn clean install either using ctrl-shift-p and execute maven command, or by running a build from Maven View/Lifecycle entry. A terminal is launched and it will inherit the system environment variables. In order to avoid this problem, I always cancel the launched maven command in the PowerShell terminal by pressing ctrl-c and change the environment variables using:
$ENV:Path="C:\apps\jdk1.8.0_351\bin;"+$ENV:Path
$env:JAVA_HOME="C:\apps\jdk1.8.0_351"
... then run the maven goal again.
I also added a favorite in Maven View/Favorites to launch the maven goal as follows:
"maven.terminal.favorites": [
{
"command": "clean install \"-Dmaven.test.skip=true\"",
"debug": false
}
]
When I run the above goal, it will always inherit the system values, and I have to change them again as mentioned earlier.
In addition, if I try to run a goal from Mave View/Plugins/Install by right-clicking and clicking debug, it will always launch a new PowerShell terminal which will inherit the system environment variables, and I didn't find a way to change this.
I am thinking that VS Code should set the environment correctly when running Maven Goals based on the JRE configured in VS Code Workspace setting.json file, but this is not happening.
When I configured Debug Request in launch.json, it is respecting the JDK defined for the project, so why it is not respecting this setting when running maven goals?
I think I can solve this problem by configuring a task that will run the maven goal in PowerShell script by I think this is a bit complex for such a simple requirement.
How I can solve this problem without configuring a new task? I am thinking there is a config somewhere to set JRE for Maven Goals for a given project.
There are various ways to set the target jdk version required to generate the jar file but all such methods will affect the class files only. You can set the target jdk using toolchain, compiler plugin, and a couple of other methods. But to generate the jar using a certain jdk you must set the PATH environment variable before running the maven goal to generate the JAR file using the intended JDK version. For example, ccording to the documentation here, maven.apache.org/guides/mini/guide-using-toolchains.html, the maven-jar-plugin is not included. I checked the JDK version in the generated .class files using the javap command and found out that the java version is affected by setting maven-compiler-plugin accordingly but the jar was generated using the javac.exe that was visible as per the PATH environment variable.

Using Maven for Java program invocation

Background
I am a Maven newbie and I greatly like the fact that Maven knows where to pick up all the JAR files needed for executing a Java program (i.e. the fact that the required JARs need not be specified in the CLASSPATH environment variable since they are all stored in the .m2 repository).
I thought Maven could be the preferred way to invoke Java programs for real world applications but for a previous post of mine the following comment was received:
If you want to execute java programs I would suggest to create self running artifacts instead of using maven to execute a program
This brings me to the following questions:
Questions
Should using Maven as the tool for invoking Java programs be frowned
upon?
If yes (i.e. there are issues in using Maven as the
preferred method for Java program invocation), what are the better
alternatives and why?
Maven is a build tool and was not designed to run programs.
If you want to test your program, your IDE should be enough to start it without any classpath hassle.
If you want give the program to someone else, use the Maven assembly plugin or the Maven shade plugin to bundle the jars with the dependencies. Then you can run it on any computer with Java installed.

Auto-install Eclipse Lombok plugin - Java

We just started using Lombok plugin for Java.
Developers in our company use Eclipse for workspace and we have some clients who get access to part of our source code.
If we use Lombok library, it doesn't get detected in Eclipse until the plugin is manually installed.
It's hard to send a communication to multiple clients and have them mandate the plugin installation.
Is there a way to automatically install a plugin from the project itself, may be with an yes/no question to let the users accept it, that way we don't get accused of sneaking stuff in :)
It is NOT possible to install an Eclipse plugin from the project itself.
It is clearly stated in the Lombok documentation that manual installation is necessary as Lombok is a preprocessor. Lombok needs to be installed to the Eclipse install folder (next to eclipse.ini). https://projectlombok.org/setup/eclipse
And I suppose it would be a security leak (both in Eclipse and your Customer's environment) if you are able to install something into the 'Program Files' folder from your projects without user interaction. However manually copying is feasible and could be a workaround if you are very keen to do so.
Workaround: You might provide an ANT/GRADLE script that will distribute you local copy of the lombok.jar into Eclipse's program folder. And also create a launcher configuration. So you can refer to this launcher step to be executed. Please note that you need to restart eclipse just after lombok installed. But in fact this is what lombok.jar does when running as described in the install guide.
The standard lombok installation is being done manually.
Therefore, I've created an automatic installer.
It's available here: https://github.com/zorik9/lombok-automatic-installer
Right now it supports only eclipse IDE in windows machine.
The idea is to configure once the variables: lombok_version, eclipse_home and workspace_dir (not mandatory)
And based on this configuration, run the installation script on each machine.
For more details, please read the readme.md file.

Problems generating ant scripts for java when linking to Rhino

When I try to build my project using ant, I get:
"java.lang.NoClassDefFoundError: sun/org/mozilla/javascript/internal/Scriptable"
This stems from my usage of Rhino (the bundled java-script that comes with Oracle JDK)
When I build the project with eclipse, everything works fine.
The ant file I am using was generated by eclipse, and works fine except for the java-script dependencies.
These dependencies are located in the RT.jar that comes with the oracle JDK.
I have tried both jdk6_0_u41 and jdk7, and both give the same results: success in eclipse, fail with ant.
I have tried to build on both ubuntu 10, and 12
I have seen two other suggestions which seem unacceptible to me:
One thread suggested copying RT.jar into the project lib directory. (RT is entire java runtime! )
Another thread suggested that I shouldn't use:sun/org/mozilla/javascript/internal. But since I am doing advanced manipulation (Calling object methods, registering callbacks, etc) I see no alternative to using the sun.org.mozilla.javascript.internal family of classes.
A comment from Oracle's site:
Users should not write code that depends on internal JDK implementation classes. Such classes are internal implementation
details of the JDK and subject to change without notice.
This is not a compiler issue. javac is behaving correctly, according
to the information provided in ct.sym.
The issue belongs with those who decide what should be available with
(and what should be hidden by) ct.sym
You may suggest to include Rhino's jar to your project and repoint it on original's Rhino classes where 'internals' are not hidden from users

Checkstyle & Findbugs Install

I have javac version 1.6.0_16 already installed on Windows XP and I'm using both Dr.Java and command prompt to compile and run Java programs.
I downloaded and extracted Checkstyle 5.5 and Findbugs 2.0.1. I'm trying to install Checkstyle and the instructions stated that I need to include checkstyle-5.5-all.jar in the classpath.
My question is, should I place the Checkstyle directory in the lib folder of the jdk1.6.0_16 directory and set the classpath as follows:
C:>set classpath=%C:\Program Files\Java\jdk1.6.0_16\lib\checkstyle-5.5\checkstyle-5.5-all.jar
Is this correct? Should I do the same for Findbugs? Thanks in advance
EDIT: When I added the above path using the environmental variables, and ran checkstyle hello.java, I got the error: 'checkstyle' is not recognized as an internal or external command, operable program or batch file
Maven will solve this problem for you
It sounds like you're just getting started in the world of Java. To that end, I'd suggest that you look into Maven for your build process. Also, you should be using at least JDK1.6.0_33 at the time of writing.
Essentially, Maven will manage the process of running Checkstyle, Findbugs (and you should also consider PMD) via standard plugins against your code. It will also manage the creation of the Javadocs, linked source code and generate a website for your project. Further, Maven promotes a good release process whereby you work against snapshots until ready to share your work to the wider world.
And if I don't use Maven?
Well, just create a /lib folder in your project and stuff your dependencies into it. Over time you will create more and more and these will get intertwined. After a while you will enter JAR Hell and turn to Maven to solve the problem.
We've all been there.

Categories