I am using Hot Code Replace feature when Tomcat is running from eclipse and it works great.
But, how can I do this manually when Tomcat is running outside eclipse?
After some searching, I have found that I need to use an agent like HotswapAgent. But, they are using this agent with modified JDK called DCEVM. I don't want to use modified JDK. I want to achieve the same thing with OpenJDK.
I know that modification will be limited to method body only but, that's not a problem for me. How can I achieve the exact same thing eclipse is doing for Hot Code Replace for an externally running Tomcat without using IDE?
Edit : Eclipse example is just to clarify what I want to achieve. I do not want to use eclipse at all. I just want to do Hot Code Replace in an application running in Tomcat.
Yes, it's possible to perform Hot Code Replace in a running JVM. This involves several steps.
Prepare (compile) the new version of classes you want to replace. Let's say, you want to replace org.pkg.MyClass, and the new version of this class is located at /new/path/org/pkg/MyClass.class
Create a Java Agent that uses Instrumentation API to redefine the given class. Here is how the simplest agent may look like:
import java.lang.instrument.*;
import java.nio.file.*;
public class HotCodeReplace {
public static void agentmain(String args, Instrumentation instr) throws Exception {
Class oldClass = Class.forName("org.pkg.MyClass");
Path newFile = Paths.get("/new/path/org/pkg/MyClass.class");
byte[] newData = Files.readAllBytes(newFile);
instr.redefineClasses(new ClassDefinition(oldClass, newData));
}
}
Compile the above agent and pack it into .jar with the following MANIFEST.MF
Agent-Class: HotCodeReplace
Can-Redefine-Classes: true
The command to create HotCodeReplace.jar:
jar cvfm HotCodeReplace.jar MANIFEST.MF HotCodeReplace.class
Load the agent .jar into the target JVM. This can be done with Attach API or simply with jattach utility:
jattach <pid> load instrument false /path/to/HotCodeReplace.jar
More about Java agents ยป
When opening any Java project, or maven project, new or old, Java throws "Error: Could not find or load main class App" at runtime. Java will only run if "package foo;" is commented out i.e. "//package foo;".
I am using the code runner extension, which works with every other language. It runs as expected in java if the package statement is excluded or commented out.
This is able to be replicated in every new java program created or opened inside VS Code, even projects created in IntelliJ (Which run as expected) and opened in VS Code.
Environment:
Operating System: Mac OSX Mojave 10.14.4
JDK version: 1.8.0_202
Visual Studio Code version: 1.33.0
Java extension version: 0.6.0
I could never get my java code to run inside VS Code, until I removed the package statement (commented it out). Once the package statement is removed my code runs as expected, but has a 'problem' inside VS Code saying "The declared package "" does not match the expected package "app"Java(536871240)"
//package app;
public class App {
public static void main(String[] args) throws Exception {
System.out.println("Hello Java");
}
}
Example1
Example2
Make sure you've already setup the JDK environment in the right way
Try to clean your workspace:Clean the workspace directory
Make sure to reload a new workspace after you finished all the above steps.
I fixed by using the above steps when I have this error. Hope it can also works on your side.
For a project I am working on, which depends on vaadin-6.8.12, I would like to use the Page/JavaScript classes included in the vaadin-server-7.0.6 JAR.
However, when I include vaadin-server, I get a java.lang.VerifyError when attempting to use the Page.getCurrent() method.
To illustrate this, I wrote a small test program which exhibits the same behaviour:
import com.vaadin.server.Page;
public class Main
{
public static void main(String[] args)
{
Page page = Page.getCurrent();
System.out.println(page);
}
}
I have set the classpath in a file (the JAR files and Main.class are in the same folder):
vaadin-6.8.12.jar:vaadin-server-7.0.6.jar:vaadin-shared-7.0.6.jar:vaadin-
shared-deps-1.0.2.jar:vaadin-theme-compiler-7.0.6.jar
Then run the program like this:
java -cp $(cat vaadin.classpath):. Main
When run, I get the following error:
Exception in thread "main" java.lang.VerifyError: (class:
com/vaadin/server/Page, method: getJavaScript signature:
()Lcom/vaadin/ui/JavaScript;) Incompatible argument to function
at Main.main(Main.java:7)
However if I reverse the order of vaadin-6.8.12 and vaadin-server-7.0.6, I do not get the java.lang.VerifyError.
I have tried the same test for different versions of vaadin and vaadin-server, always with the same result as above.
vaadin is not listed as dependency of vaadin-server (and vice-versa) in the maven POM. Im am using JDK version 1.6.0_32. Does anyone know what could be causing this?
Many thanks.
You cannot combine Vaadin 6 and Vaadin 7 that way. If you want to use features from Vaadin 7 you need to migrate the whole application to use Vaadin 7.
If you want to execute some JavaScript from server side, Vaadin 6 has the Window.executeJavaScript() method that you can try to use.
I have several launch configurations in Eclipse each launching the same Java program but with different parameters.
Now is it possible to run all of these at once (with one mouse click) instead of selecting each of it separately and launching it?
EDIT: According to this answer since Eclipse Oxygen (4.7.0) you can use a run configuration of the type Launch Group for that.
Just install "C/C++ Development Tools" from the CDT (see eclipse.org/cdt/downloads.php ) - this single package is enough, no other CDT packages are needed. This won't disturb your Java environment ;-) Then you have "Launch Groups", for any kind of project, including Java projects. See the following screenshot:
You can run or debug the projects (also mixed mode), define delay times and so on. Have fun!
I found this post on the Eclipse trackers: Start multiple debug configurations at once
While it talks about multi-launching debug configurations, I think it is just as applicable to run configurations.
You may want to right click a run configuration in group launch and configure it.
Since Eclipse Oxygen (4.7.0) you can use a run configuration of the type Launch Group for that.
This short video shows how to use a Launch Group.
There are two more options listed in Launch an Eclipse Run Configuration from ANT.
You could group them in Ant and then call them using Ant4Eclipse. Or call multiple launch configs from a command script using eclipse remote control.
You can create a separate class that calls your program with different arguments, and run it instead.
public class YourClass {
public static void main(String arg){
System.out.println(arg);
}
}
public class YourClassTester {
public static void main(String[] args){
YourClass.main("SomeArg1");
YourClass.main("SomeArg2");
YourClass.main("SomeArg3");
}
}
You don't need any plugin:
Create all Run Configurations in eclipse
Select Organize Favorites...
Add you favorites, done
I am executing a junit test case
I got the following error,
A fatal error has been detected by the Java Runtime Environment:
Internal Error (classFileParser.cpp:3174), pid=2680, tid=2688
Error: ShouldNotReachHere()
JRE version: 6.0_18-b07
Java VM: Java HotSpot(TM) Client VM (16.0-b13 mixed mode windows-x86 )
Can any body please suggest the solution to resolve
I got the same problem, but with alot of googling I found the answer! See this page
Quote from the link:
# An unexpected error has been detected by Java Runtime Environment:
#
# Internal Error (classFileParser.cpp:2924), pid=5364, tid=6644
# Error: ShouldNotReachHere
That's because we are using Android's JUnit stub implementation. Go to Run -> Run As -> Run configurations again and in the recently created JUnit configuration Classpath's Bootstrap Entries remove Android Library
Then Add Library, using Advanced... button, and add JRE System Library and JUnit 3
Apply and Run
Try this, it worked for me.
You'll need to take this up with Sun -- looks like a JVM bug to me. If it's reproducible, you should be able to run java in such a way as to generate more details (e.g. -verbose, etc). If you can reduce it to a minimal case that triggers the bug (source code always helps!), that also goes a very long way.
http://java.sun.com/developer/technicalArticles/bugreport_howto/index.html
http://bugreport.sun.com/bugreport/crash.jsp
In the meantime, you might want to try it with a different JVM implementation (maybe even an older patch level of the Sun JRE).
Go to Run As -> Run Configurations... and select the configuration you are using.
Select the Class Path tab and select BootStrap Entries.
Click on Advance, then Add Library and select JRE System Library.
Bring it up and make it the first entry in the BootstrapEntries List.
Apply and Run...
Another possible explanation: hardware failure. Ruled out if you can reproduce the error on different machines.
I resolved this by
Quit eclipse
Delete the bin and gen directories in your project
Start eclipse
Rebuild your project
I just recently found solution for this issue that was posted by devdanke:
"As of 11-July-2010 and Android 2.1, the work around I use is to segregate tests into different classes. Any test(s) that don't call any Android APIs go into their own classes. For each of these classes, I remove the reference to Android in their Run Configurations, Classpath tab."
The problem with having it configured class by class is then is not possible to run all tests in project. Better approach is creating 2 test projects with different sets of libraries.
Standard Android JUnit Test project can be created following link, and sample test class looks like:
import android.test.AndroidTestCase;
public class ConverterTest extends AndroidTestCase {
public void testConvert() {
assertEquals("one", "one");
}
}
Then JUnit Test project can be converted from Android JUnit Test project by removing Android Library from project build path, and adding JRE System Library, and JUnit 3 library, and sample test class looks like:
import junit.framework.TestCase;
public class ConverterTest extends TestCase{
public void testConvert() {
assertEquals("one", "one");
}
}
I have had a similar problem, I found it was because I had generated a new activity with a main[] stub entry. Once I deleted the main[] code from the new activity templatye the error went away.
YMMV
This could be a JVM bug; see #Zac's answer. But it could also be that your junit test case is causing a corrupted bytecode file to be loaded. Try rebuilding all your .class files, and if that does not fix the problem try refetching any external libraries that your code depends on.
Do you run on a supported platform (Windows, one of a few Linux versions?) If not, that is the first to try.
If you ARE on a supported platform, then downgrade to _17 and see if THAT helps.
Then make a bug report to Sun and hope they will fix it someday (unless you want to give them money for fixing it faster).
Go to Run As -> Run Configurations->classpath->BootStrap Entries
Click on Advance, then Add Library and select JRE System Library as a first entry.
Apply and Run...
I am not sure whether you were able to reach the solution for your problem or not but your question just popped up while I was searching for the solution for the same problem I am facing. And I got one solution from the stack itself, so just thought to share a link with you if that aids you by any means. The link is as below:
Can't run JUnit 4 test case in Eclipse Android project
Another possible reason (for future references):
I had accidentally copied in a main method in my code, causing Eclipse to recognize the project as a java application, thus launching it with that configuration.
To solve it I went into Run > Run Configurations... and then changed from my presumed main in java application to the main activity of my android application simply by choosing it in the left column.