I have a java class and I need to debug it (put breakpoints and continue using F6). I am using ANT script to init, build, deploy and run the code. I am using:
<javac srcdir="${src.dir}" destdir="${classes.dir}" debug="true" debuglevel="lines,vars,source">
..........
</javac>
But when I place the breakpoint in a line in my foo.java class and I run the ant script (the run part, Right Click on run-->Debug As-->Ant Build), Eclipse does not stop at that line of code.
What am I missing out?!
(Wasn't able to comment on the given answer, so have to make another answer)
I realized that when launching Ant from Eclipse, you'll have to add fork="true" to the <java> task. Also, it was first not clear to me how to write nested jvmargs, so here goes an example:
<java classname="..." fork="true">
<jvmarg value="-Xdebug" />
<jvmarg value="-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5432" />
...
</java>
In the <java> ant task you should add two jvm parameters (<jvmarg> IIRC) to turn on debugging:
-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5432
This will launch the java program with debugging turned on and the program will be ready to accept debugger connections on port 5432. Then you should use your IDE's remote debugging facility and direct it to connect to port 5432.
In Eclipse:
Toolbar > External Tool Configurations... > (select your existing ANT build or create new) > JRE tab
In "VM Arguments" add:
-Xdebug -Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=y
Again Toolbar > Debug > Debug Configurations... > Remote Java Application > New
Name: Debug Ant
Project: <Select your project where debug files are kept>
Host: localhost
Port: 8787
Now in "External Tool Configurations" launch "ANT Task" (which waits for the Remote Java Application debugger to connect), then launch the "Debug Ant" from the "Debug" toolbar icon.
This is how I got it working for me (Just commenting for future reference).
Link dump ahead :
Debugging ant tasks is not as simple as plain old java debugging.
While you can debug an Ant file adding breakpoints, digging inside the
code of specific custom task will require you to add a remote debugger
in order to be able to "catch" the running process.
I will explain how to do this in Eclipse, altough I recon it can be
achieved with all major java IDEs. First thing is to create a new run
configuration for the ant file where you plan to use your customized
new task. To do so, go to:
Run -> External Tools -> External Tools configuration...
Right click in Ant Build -> New and in the Main tab select your ant
script in Buildfile field. Then go to JRE tab and insert the following
JVM arguments:
-Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n
If you wonder what these arguments mean check this, although a bit
updated (Java 1.5) still works.
Once this is done, you must create a new Debug configuration for a
remote Java application. To do so, navigate to:
Run-> Debug configurations
Drop down the list in the left column and right click in Remote Java
Application -> New. Select the project name in the Project field.
Default values for host and port are okay as long as you used the same
ones for the Ant configuration (JVM arguments).
Everything is ready for the test run! Add breakpoints wherever you
consider necessary. In my case, I added one both in the ant script
that uses the custom ant task as well as in the custom ant task, in
the execute method.
Right click in your ant script or task -> Debug As...-> Ant >Build
first
Now BEFORE calling your custom ant task code, go to Run-> Debug
Configurations and debug your previously created Java Remote
Application config. This will start a separate thread that will debug
your custom ant task code, provided that you included some breakpoints
:) You can see in the following image how in my case, thread stopped
in the execute method of my custom ant task.
After this point, it is up to you to decide what to do next...
This is to help the people who are wondering how to debug the web application that use ant to build and deploy. This is quite frequent in legacy applications. If the project was started as "Dynamic Web Project" as the beginning, following steps and even Ant is not necessary.
Set the break point in your code.
Window -> Show View -> Others -> Servers
Add your server JBoss or Tomcat for example.
Right click on the server and choose 'Debug'.
Make sure that debug="true" is set in ant build file.
set ANT_OPTS=%ANT_OPTS% -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5432
in Eclipse
Toolbar >> Run >> Debug Configurations >> + >>
Give the values:
Name: Debug_Ant
Project: active-eclipse-project
Host:localhost
Port:5432
Steps:
1)Configure remote java debugger with local host as name, port address as 8000(or whatever your system's port address will be)
2)Create a batch file and keep that in bin folder of your tomcat(this step is required when we want to debug remotely keeping server/s in same system).
in batch file you should keep this line:
set JPDA_ADDRESS=8000
set JPDA_TRANSPORT=dt_socket
catalina.bat jpda start
after this keep a break point in your java code, and debug this remote debugger. it will work.
Happy Coding !!
I too faced this problem, I did following steps to resolve.
Put the below lines in ANT file
Go to the debugging configurations->Remote java application-> Create new configuration file with project name,port=5432 and host is localhost and save it.
Now run your build.xml using debugging mode, then you should see in console that "Listening for transport dt_socket at address 5432"
Now run debug configuration file which is you configured. Now your selenium code will run using Debug mode.
Hope this helps.
If you still facing issues, please let me know so that i can help you on that.
Thanks
Related
I'm using the Gradle wrapper in a project in Idead. I have an init.gradle file in a non-standard location and need to find a way to specify it in Idea. I thought this could be done via the Gradle VM options setting field and entered --init-script /path/to/init.gradle in it, but this throws an Unrecognized option --init-gradle /path/to/init.gralde error when trying to re-import the project.
Is there some other way to do this that I'm not aware of?
You can set Gradle commandline options/arguments from the "Run" configuration panel:
If you set this option on the "Templates>Gradle" panel it will apply to all Gradle tasks invocation ( note that you need to execute tasks with Right-click/Run from the Gradle tool window: double-click will not take the option into account..)
EDIT : another way is to add the commandline options directly in the "Run gradle task" window:
I use Jenkins to run regression periodically
I have java-maven project with 'ATC.properties' where I can choose browser, environment etc. by uncommenting appropriate one
#### browser ######
browser.name=firefox
#browser.name=chrome
#browser.name=ie
So I have to commit it, push and only after that job on Jenkins will run build with chosen parameters in 'ATC.properties' as well
How can I make my maven project read parameters from parametrised Jenkins build.
Can any one give me some example with browser ?
Do I have to use another one '.properties' file with described variables
like
browser.name=${browser.name} ...
in my project ?
Parameters defined in Jenkins will be expanded at run in Maven Build , Below process does not require additional property file :
Define jenkins choice parameter :
browser_name
Provide all your Browsers options as choices and select the required option at run time.
Now replace your pom.xml with ${browser_name} where you required the option of reading browser value instead of reading value from property file.
pass parameter at run time as below
mvn clean install -Dbrowser_name=%browser_name% [incase of windows]
mvn clean install -Dbrowser_name=$browser_name [incase of linux]
Is there a way to set System Properties which are used for every JVM that is started by NetBeans (NB 7.3.1 on Win 7)?
In all my maven projects I use Log4j wich needs a log4j.properties file, to where I want to point to by a System Property -Dlog4j.configuration=file:/c:/log4j/log4j.properties
I could set Global Execution Options for Maven: Tools > Options > Java > Maven > Global Execution Options. But then when I run a particular JUnit test class in NetBeans (thus without Maven), NetBeans won't use these JVM System Properties set in the Global Execution Options field.
Same issue is when a Main class in a Maven module is run in NetBeans. To set JVM System Properties there are Project Properties which could be set: 'Main Class' and 'VM Options'. But these Project Properties are only used by NetBeans when the 'Run' command is used from the context menu of the maven module. (Unfortunaly this menu item doesn't have a shortcut key (normaly Shift-F6)).
I have worked around this by setting a System Variable JAVA_TOOL_OPTIONS=-Dlog4j.configuration=file:/c:/log4j/log4j.properties.
Edit
A question was asked for the same problem I ran in to: How to make Netbeans use specific JVM parameters when running tests? In my case the problem was caused by the option Compile on Save which I had swiched on (File > Project Properties > Build > Compile > Compile On Save). In that case it seems that NetBeans doesn't use Maven. For further info about CoS: http://wiki.netbeans.org/FaqCompileOnSave
There's a file called project.properties in the nbproject folder. In this file there is a lot of configurations you can do that are not in the GUI menus. I don't know exactly what to put there to achieve what you want, but I found this on one of my NetBeans projects config:
# Space-separated list of JVM arguments used when running the project
# (you may also define separate properties like run-sys-prop.name=value instead of -Dname=value
# or test-sys-prop.name=value to set system properties for unit tests):
run.jvmargs=
run.test.classpath=\
${javac.test.classpath}:\
${build.test.classes.dir}
Sometimes I am doing simple fixes for rather huge Java application and I don't want to open Eclipse for this task. Eclipse starts long and since the project is build out of large number of subprojects, which are build anyway by Maven, it takes ages before Eclipse is usable (at least ages in impatient Java developer scale).
Almost everything I need can be done in Sublime Text editor, however one place where Eclipse shines is debugger. My workflow is: make a fix, then test it running application (on server) using debugger, to check if everything is ok.
So is there any Sublime plugin, or other non-IDE solution for easy debugging of Java application.
Note: I have seen this post - its pretty old, maybe there is something better.
You could look for a standalone Java Debugger like JDebugTool.
Or you could simply create an additional Eclipse workspace with only the bare minimal you need for remote debugging your application (just one project with all the source jars linked in).
This workspace will start considerably faster than the full blown.
I have never worked with a standalone debugger for Java and personally I tend to keep the number of tools I have to learn as low as possible and Eclipse's debugger is pretty good.
JDebug - A Java Debugging plugin for Sublime Text
Debugging is a painful art. The programmers(coders) know about it. Even perfectly written code may misbehave sometimes and it is difficult to identify the piece of code causing the issue just by log statements. So the only possible way to identify the issue is to do step by step debugging of the code dynamically. There are so many debugging tools available for Java, some are standalone and few are integrated within the IDE like Eclipse. In this article, I will show you how to debug java codes remotely using JDebug in Sublime Text.
Setting up Sublime Text JDebug plugin can be installed using Package Control or manually. I will show you how to install JDebug plugin using Package Control. If you haven’t install the Package Control, you can install packages by installing package control.
Once you have the package control installed, you should start Sublime Text. Open up the command palette from the Preferences --> Package Control menu and search for “Install Package”.
Now you can search for any package you like. In our case, we are going to search for the package “JDebug”.
Setting up App Server (Weblogic/Tomcat/any other) in debug mode
Pass -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8000 as JVM argument to server startup command. You can change the debug port from 8000 to anything you prefer.
Tomcat
Add/update the JAVA_OPTS env variable in catalina.bat or catalina.sh set JAVA_OPTS=%JAVA_OPTS% -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8000
Weblogic
Add/update the SAVE_JAVA_OPTIONS env variable in catalina.bat or catalina.sh set SAVE_JAVA_OPTIONS=%JAVA_OPTIONS% -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8000
Setting up JDebug Setting up JDebug is very simple as it has only few settings to start with. You can copy and paste the default settings into user settings and update the following settings based on your environment.
workingdir - You need to set your current project directory like c:/worksapce/TestService (Windows) or /home/user/abc/workspace/TestService (Linux or Ubuntu)
commandline - Set the jdb command with arguments. If JDK/bin is not in your environment PATH then you need to specify full path to the jdb command. Also change the hostname and port in which the java application service is running and listening.
sourcepath - The path to the source file with in the project. For ant project it usually /src/ and for maven usually /src/main/java.
Breakpoints Ready to set the breakpoint?. Breakpoints are the way to inform the jvm to halt the execution at a particular point (line number or method in a class). When the execution halts, the application variables can be inspected.
Add a breakpoint using 'Toggle Breakpoint' menu option from context menu. Context menu can be accessible using right click. A circle icon will be placed on the line number gutter when a breakpoint is added
When the jvm hits the breakpoint during the execution, the gutter icon will be changed to 'pointer' to indicate that the breakpoint is hit.
Inspect Variables
Variables & expressions can be inspected using inline popup. Click on the variable or highlight the expression to evaluate/inspect inline.
Watch Expressions Expressions can be evaluated using the 'Add Expression' context menu. You can enter any valid expression to evaluate.
Enter the Expression
JDebug Variables It is interesting to watch the variables in the 'JDebug Variables' window. If a variable is complex object it will be displayed with '+' icon on gutter. These variables can be expanded further to get the additional details using 'Expand' context menu. The 'Expand' context menu will be enabled only in 'JDebug' variables window.
How to Continue?
The application execution can be continued using one of the following menu options
Step Over Continue execution to next line
Step Into Continue the execution into a method call
Step Out Step out of the current method and continue
Continue Continue to next breakpoint or till completion
enter image description here
I'm using Eclipse; when I run my code in debug I get a class not found exception but running it without the debug it works fine.
I see it doesn't find the classes I wrote.
How can I fix this?
Double-check that the Run Configuration in Eclipse has the same classpath set for both (running it normally vs debug).
Eclipse has a lot of settings but I would try checking the following:
Run > Debug Configurations > Java Application > PROJECT_NAME > Classpath
Also, you can check your source under the same location to make sure all of your JAR files have been properly added.
I assume we're looking at a normal Java Application. Eclipse creates Run Configurations for normal execution and Debug Configurations for debug runs. And there's usually no significant difference between a Run and a Debug configuration for the same class.
The easiest way would be to start clean: Navigate to 'Run Configurations...' and delete all 'Java Application' configurations. Then do the same for 'Debug Configurations'. After this is done, start your application once in normal and once in debug mode.
Alternative: switch to a new workspace (File -> Switch workspace), import the project(s) (File -> Import... , make sure to copy content) and run/debug your application.
Check your run/launch configurations. You probably have a different configuration for debug vs run. Chances are that the debug one isn't pointing to the main class of your program.
Just check the debug... option (pull down the debug menu don't click the button, then click debug...) and the first page should have a field for "Main class". Use the same main class as in your run... option.