How to hot reload Tomcat server in VSCode - java

I'm migrating from Eclipse IDE (+ VSCode for coding Java servlets and HTML/CSS/JS webpages, respectively) to only Visual Studio Code for its lightweight.
I have several Java extensions installed for VSCode:
Language Support for Java(TM) by Red Hat
Tomcat for Java
Debugger for Java
Eclipse has a series of settings for hot reloading:
- Automatically publish when resources change
- Refresh using native hooks or polling
while VSCode doesn't seem to have any for me.
A few things I've try to reload my Java and web code:
Restart Tomcat server
Delete and re-adding Tomcat server
Delete and regenerate .war package (not sure if this does anything, it can run well without a .war package)

Good news...
It works automatically now. With Tomcat for Java Extension (And the rest of the Java Extension Pack):
https://marketplace.visualstudio.com/items?itemName=adashen.vscode-tomcat
Just make sure your settings are like this (search for "java.autobuild" to get those two in the first results):
Very important is, that you wont see any logs in the console on the HCR (Hot Code Replacement) like it happens in Eclipse ... but you in fact will see the replaced code behavior. Just debug over the piece of code you changed, and you will see it in fact changed in the running server.
UPDATE: I've found it works better with the 'manual'(default) setting. Just clicking once in the lightning icon. (Testing in Ubuntu 18.04 LTS)
[I say it works better because if I added lines to a Class' code with the 'auto' setting it will not add that code ... only if I change code in the existing lines. But with the manual setting I just save the file, and then click the lightning icon wait ~3 seconds myself and debug over the new code ... and everything goes well!!
... This solves my coding needs(lightweight Editor/IDE with Hot-Code-Replacement in Tomcat)]
Enjoy !

This article may be helpful, I migrate from other IDE to Vscode.
According to the article, if you install Debugger for Java, it enabled Hot code replacement(HCR) and then :
You can start a debugging session and change a Java file in your development environment, and the debugger will replace the code in the JVM running your code.
Hot code replacement for java comes to visual studio code

Install “Tomcat for java” extension in VScode.
Configure the Path of Tomcat in the above extension.
Now you should be able to run tomcat in usual build -> deploy mode. Now install DCEVM, By using “java -jar installer-light.jar”. (Select “Install DCEVM as altjvm”)
Place HotSwap Agent Jar in directory of your choice.
Now In VScode right-click the Tomcat server you have created. (The one from Step 3 ) And select “Customise JVM Options”.
In the file opened, Place “-XXaltjvm=dcevm
-javaagent:/[your_directory]/hotswap-agent-1.3.1-SNAPSHOT.jar” Now Right-click the tomcat server in VScode and select “Debug WAR package”, And select the WAR file.
If everything goes well, The WAR will be start in TOMCAT in debug mode with text “HOTSWAP AGENT” in the log.
Now your every save to your file will trigger Hot Reloading.
https://medium.com/#manoj_makkuboy/hot-reload-java-8-tomcat-server-in-vscode-ba6233d632e?

Related

How to save Java Project from IntelliJ IDEA to Git repo so it will build on another PC after cloning?

I'm building a simple calculator app from a tutorial/class on Pluralsight (Java Fundamentals class), which starts with a Main.java file, and adds a class file later, MathEquation.java.
I created a Git repo to track my progress, and uploaded to Github: JavaFundamentalsClass. I used GitIgnore.IO to find a Java IntelliJ .gitignore file, and everything seemed to be working fine on the original PC I was using.
However, after switching over to a different PC, with a fresh install of IDEA, I cloned down the repo from Github, and found that the project would not build and/or run. I get this error:
"C:\Program Files\RedHat\java-1.8.0-openjdk-1.8.0.191-1\bin\java.exe" -Dfile.encoding=windows-1252 -jar ""
Error: Unable to access jarfile
Process finished with exit code 1
I suspect that there is something in the .gitigore file that is keeping some of the project, dependency, or build information from being saved in the repo that keeps IDEA from having all the info it needs to build and run the Main.java correctly.
Can anyone help? You should be able to download the project from the repo linked above in it's currently broken state, including the .gitignore file.
Additional Info
I wiped my repo clean and recloned and I forgot, originally it doesn't even give me the run button (it is grayed out). I had to do a procedure with "Add Configuration" from a search I did for that to show up to get the error above.
Also, I did install java from the OpenJDK, rather than oracle. I figured with Oracle cutting off commercial development maybe start the switch now as I'm learning, but maybe that's the issue (normal Java on my other PC).
It's working, but...
First thing - Intellij needs to know where Java is. It appears that if the SDK/JDK is not installed prior to Intellij, you will need to tell it where Java is after installing. Or if you use OpenJDK instead of Oracle Java SDK. You can do that at View/Open Library Settings, under Platform Settings / SDKs (select the JDK home path).
Then, after setting up Java, I am able to get everything working in Intellij IDEA if I find the Main.java file, right-click, and choose "Run main.main()". This runs the program okay, and also creates a Main configuration in the Run/Debug configuration area, and finally the "Run" button becomes available.
So I can get it working, but if I wipe all the files and clone it fresh from the remote repo, I have to go through running the Main.java file directly again to recreate the configuration.
So, this still doesn't answer my original question. Why doesn't this configuration get saved in the repo? What file is this information saved in? Is there something in the .gitignore file that is keeping this information from being saved to the repo?
Look like you have a different java version on your second machine.
First of all, check your java version
Update the configuration of your project, right now it pointing to java 1.8
https://github.com/LightCC/JavaFundamentalsClass/blob/master/.idea/misc.xml
I think it might be because of java not installed on the other PC you were trying. If it is installed just check whether the path mentioned has java executable file.
The first part is ensuring Java is setup, and that Intellij IDEA knows where Java is, per the other answers, comments, and addendums to the question.
The second part is setting up the run/debug configuration, per the "It's working but.." section of the question. This can be created automatically by right-clicking the file that has the main class to be run (usually Main()...), and selecting the "Run Main.main()" option (replacing Main and main() with the file and function which needs to be run)
The final piece is that to get the run/debug configurations to save to the repo, you either need to:
Share the workspace.xml file (i.e. make sure this is not in the .gitignore file). However, there is a lot of user-specific stuff in that file, so it should normally be excluded from the repo.
Or, edit the configuration and checkmark the "Share" box in the upper right-hand corner (just right of the "Name" field). This will put the configuration into a separate folder inside the .idea folder, which should not be excluded from the repo by .gitignore.

How to auto reload app engine dev server?

Im following the tutorial on the App Engine website for 'Google Cloud Endpoints' in Java. Everything works fine and I can run the development server using mvn appengine:devserver. The problem is that when I make any changes to a file (for example, any java file) the dev server doesnt automatically recompile. I need to ctrl-c to kill the dev server and restart it for every code change I make.
Is there a way to have maven automatically detect changes to any files in my project and have it automatically rebuild and restart the dev server?
Unfortunately no. If you want this behavior on your dev server, you need to use Python.
I run in the same issue and there is no real workaround provided by the App Engine to help you doing this.
From the "Using The Google plugin for Eclipse":
With Eclipse, you can leave the server running in the debugger while you make changes to source code, JSPs, static files and appengine-web.xml. When you save changes to source code, Eclipse compiles the class automatically, then attempts to insert it into the running web server dynamically. In most cases, you can simply reload the page in your browser to test the new version of the code. Changes to JSPs, static files and appengine-web.xml are recognized by the development server automatically, and also take effect without restarting the server. If you change web.xml or other configuration files, you must stop and start the server for the changes to take effect.
(https://developers.google.com/appengine/docs/java/tools/eclipse#Running_the_Project)
There is NOTHING comparable in Java (link from "The Java Development Server") (https://developers.google.com/appengine/docs/java/tools/devserver)
There's currently nothing in the App Engine SDK to automatically restart when files change, but that's not to say you can't do it. I ran into the same problem and wrote up a script to listen for file changes as triggers to restart App Engine. It's in JavaScript, so you'll need to install Node.js if you haven't already.
// Install watch-exec
$ npm install -g watch-exec
// Watch the current directory
$ watch-exec --command "mvn appengine:devserver" --watch .
This will immediately start App Engine, and then restart it any time a file changes. If the app crashes for some reason, the script will wait for your next edit before trying to restart.
P.S. That entire script is about 40 lines of code, and you could probably do the same thing in other scripting languages. If you haven't tried writing your own automation before, I'd definitely recommend checking out the source code to see how this works.
I've found using Gradle, GAE, and Spring MVC, the assemble command will put the correct artifacts in place, and the server will re-init the app. It's a little quicker than a server restart.
Using App Engine standard with the cloud.tools appengine-maven-plugin hot swap works fine (most of the time, can be problems when setting up the workspace).
For a multi-module maven project: no need to stop the server or browser,
just push the code changes (maven command package -pl *-server)
& refresh the browser.
Debugging with a debug client currently works perfectly for changing / adding code within methods.

Netbeans 7.2 won't open on OS X

I installed Netbeans on OSX Mountain Lion running JDK 7. Everything was working fine. Then my machine for no reason crashed. After this I could no longer open Netbeans anymore.
If I try to start Netbeans from the commandline:
/Applications/NetBeans/NetBeans 7.2.app/Contents/MacOS/netbeans
Then it works fine. From there I can select Netbeans > About and confirm that my userdir and cachedir is:
User directory: /Users/<user>/Library/Application Support/NetBeans/7.2
Cache directory: /Users/<user>/Library/Caches/NetBeans/7.2
So, I deleted these directories and tried again, but alas, I can only start netbeans via the commandline.
Ideas appreciated...
Sounds to me like this is not a NetBeans problem at all, rather a problem with the bundle, such that OS X is not launching the app as it should.
To verify, I'd try opening NetBeans through Finder from the command line:
open -a "NetBeans 7.2.app"
This command will ask the system launcher to open the application as if you had clicked on the icon. In your case, it probably won't open. If this is true, then you know it's not something in your terminal environment that's allowing NetBeans to launch.
The next step that I would take would be to look at the package contents: examine /Applications/NetBeans/NetBeans 7.2.app/Contents/Info.plist for anything that seems amiss - this file is the information store that OS X looks for to see how to launch the package, what its icon is, etc... - in particular, I think you should look at the CFBundleExecutable value to see what command is used to launch the app. If it's not netbeans, you've found a big hint on what might be amiss: bundles are set up so that launchers should run bundle_dir.app/Contents/{platform}/{CFBundleExecutable_value}. Looking at CFBundleExecutable should show you if there's some other script used to launch the app than just running netbeans.
From this point, you'll have to do more digging, but hopefully this is enough information for you to get started tracking down how apps are launched from the OS X launcher. For more info and a better reference, check out this link, which is Apple's documentation for keys in Info.plist.
Finally, you should note that the launch information for all apps are heavily cached; I've heard that if you make a change to Info.plist, you have to rename the app bundle and name it back to have the changes picked up by the launcher.
Good luck!
Here's an idea: use Eclipse >=)
..but seriously, something got corrupted in Netbeans' metadata/cache data files. You cleared a couple of them but there may be others elsewhere.
Try comparing the launch command in your shortcut to what you're typing in the console. Are they the same?
To find the root cause, I would check the NetBeans logs, as detailed here
/Users/yourname/Library/Application Support/NetBeans/7.2/var/log/messages.log
And if that doesn't help, I'd archive your project(s) and put a clean install of NetBeans on your machine, and then import the archived project(s) back into NetBeans.

Eclipse skipping breakpoints

My issue isn't a new one, and I found several threads about it, yet no one was helpful for me.
I hope this one will.
I can't debug with eclipse. It recognizes breakpoints, switches to debug view, but somehow it gets out of sync and I can't debug my code. Sometimes It works, but after a while It happens again.
I work with JDK 1.6.0_24, Glassfish 3.1, eclipse INDIGO (but it also happens on HELIOS).
I tried (as I found in this site) to set eclipse to use Parallel GC. I tried put it in the eclipse.ini file, I tried to put in the glassfish VM arguments itself, and I tried to put it at eclipse JDK additional VM arguments (preference -> installed JRE-> JDK -> etc). Nothing seem to work for me. I also tried them all together.
Help me please..
Thank you all from advance,
Ido
EDIT:
OK, I noticed few more things:
On the Debug view, in my stck trace, I found the exact class with the breakpoint is paused and it had this comment: "(Suspended breakpoint at line XX)". When I clicked the line, suddenly the green line appeared. Yet, as soon as I clicked F6 to continue, It went out of sync again, just the next line became suspended. Weird.
I checked the processes running on my computer and found that there are several "java.exe" processes running at the same time. (eclipse runs on javaw.exe). Maybe this is why glassfish and eclipse refuse to work together.
I checked the build path and found that this is my output folder: project-name/target/classes. Is this good?
Any thoughts?
Ido
So, I couldn't fix this but I found a workaround that completely solves this - FINALLY!.
I just stopped using glassfish WTP for debugging and set a remote debug to my local glassfish. it works perfectly with few minor annoying things - but breakpoints finally noticed.
Set remote debug
Go to your glassfish admin console and set your glassfish to work on debug mode.
Click on configuration --> server-config --> JVM settings, and check debug enabled check box.
Restart server
In eclipse - start server on normal mode (not debug - it is useless).
Go to Debug configurations and locate "Remote Java Application"
Create a new Remote java app debug config
Enter name (lets say Glassfish-Debug)
Choose project to debug
Enter your own IP address in the host section and set the port to 9009
This is optional but it is more comfortable to work with:
Go to "Common" tab, and check the Debug check box. This will make this Glassfish remote debugging configuration constant on your debug menu.
That's it. Now all you have to do is always start your Glassfish in normal mode and then go to Debug configurations and run This Glassfish remote debugging you just set.
And now I'm getting to the annoying part: After rebuild your project, sometimes you might get again out of sync. You just need to disconnect the remote debugging session and run it again. Small price to pay.
I hope it helps..
I had faced similar issues, But it turned out to be a simpler problem of multiple versions of JREs/JDKs installed.
Try Debug -> Run Configuration -> JRE -> Alternate JRE -> select JDK 1.6.0_24.
Also check for project build paths if correct Jars are been used.
Hope this helps.

Flex code doesnot(only sometimes) compiles in java+flex (+ BlazeDS+Tomcat ) combined project.a

i am working on a flex+java combined project.
My IDE and computer configuration is as follows:
Windows 7 Professional, 64-bit, Core2Duo 2.1GHz, 2GB Ram
Eclipse (helios) with Flash Builder Premium v4.0 Eclipse plugin
Flex=javaEE combined project with BlazeDS v4.0.0.14931 and Apache Tomcat 6
using BlazeDS remote object--RPC for flex+java communication.
My Problem is :
Any change in java code is compiled immediately and appeared on output when i run the project on server.
When i make a small change in Flex code (eg, reposition of a button...anything), and run the project on server, it does make any change on output.
Actually it depends on my luck... because it makes change in output randomly.
what is is happening???
i tried rebuild, republish, clean , restart server, restarting the IDE, restarting the computer..... all available feature i could think. Also, i created a simple flex project on IDE and compiled it..... everything is compiled and displayed on output.
Please HELP ME.
Ganesh Tiwari
IOE, Pulchowk Campus, TU
Nepal
Try to clear your browser cache and see what happens
Please clear the Browser Cache each time you run and then try.
SWF is cached by the browser, so it does not take the new one, it displays the old copy only.
Hope this should solve your problem

Categories

Resources