http connection timesout when running from commandline - java

I developed a java code to connect to a device and issue few POST commands. This runs well in eclipse. But when I build and run on command-line it always times out.
I am running eclipse in the same machine as command line .I am using java 8.

Network problems are best tackled by first keeping Java out of it. So as a first step, you can open up the console and try to do a telnet connection to the socket address you used in your program by entering
telnet targetserver.example.com 12345
If that times out as well, the source of your problem is not within Java.
You haven't provided much (e.g. source as requested) but my guess into the blue is that you're sitting behind a proxy that is configured in Eclipse. Eclipse passes that information to the started application so the connection works. Starting the application on the console lacks this information, so the network connection is attempted directly without going via the proxy.

Related

How to debug a jar remotely from netbeans

I'm trying to figure out how to debug my jar that is running remotely. Here is my scenario:
My .jar will be running from a VPS. This jar basically runs a server
for a game, so it also connects to a mysql db. I start the server with 3 .bat files that looks something like this:
set CLASSPATH=.;dist\aries.jar;dist\mina-core.jar;dist\slf4j-api.jar;dist\slf4j-jdk14.jar;dist\mysql-connector-java-bin.jar
java -Xmx500m -Dwzpath=wz\ -Djavax.net.ssl.keyStore=filename.keystore -Djavax.net.ssl.keyStorePassword=passwd -Djavax.net.ssl.trustStore=filename.keystore -Djavax.net.ssl.trustStorePassword=passwd net.world.WorldServer
pause
What I want to do is start the server on the vps like normal, but debugging the server on my local machine via Netbeans IDE. I don't know if this is possible because people will be connecting to the server (although, I will be debugging a test server which will only have me online).
Note: I have done a lot of searching before coming here and a lot of what I found had to do with using xdebug & php which has doesn't have much to do with my situation (I don't think)
-Thanks
There's a NetBeans FAQ page about this.
In brief:
Add the remote debugging options to your Java command. For example:
-Xdebug -Xrunjdwp:transport=dt_socket,server=y,address=8888,suspend=n
Then, use the attach debugger option in NetBeans and select your server and the port you used above (8888). It's pretty much that easy.
You may want to think about network and firewall considerations, as you may have noticed there are no passwords involved, so anyone who can connect to the port can debug your app. This could be a big security risk. Your VPS provider probably has some tools to help with setting up a secure, private connection.

Analyze stand alone Java Application locally for threads

Respected Experts,
I have a stand alone java application and want to monitor the threads created by it. I am planning to use a tool like JConsole or JVisualVM. However, I am not able to connect these tools locally to my Java Application.
I am using Windows machine. JConsole and Java program are running locally. I have tried to run Java application with following JMV arguments with no success:
java -Dcom.sun.management.jmxremote.port=9999 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false LinkedListTest
When I try to connect using JConsole, my process id is greyed and following message appears:
Note: The management agent is not enabled on this process
I think I should be able to connect JConsole to a stand alone java application. Any thoughts what I am missing here
Thanks and Regards
Thanks for the inputs. I was able to solve the problem and the details are as follows:
As #Holger mentioned, both JConsole and JVisualVM can connect to the local Java Application running without any JMX arguments. The problem that I was facing was machine specific. I restarted the machine and deleted the following directory:
%TMP%\hsperfdata_User.Name
(I was not able to delete this directory without doing a restart)
Restarted JConsole/JVisualVM and was able to connect to local java processes using the process id.
In fact, the use of JVisualVM pointed me towards this fix. On start of JVisualVM, I got an error message stating something like local processes/applications can't be monitored. The message had a link to Troubleshooting guide. I am reproducing the relevant snippet:
Local Applications Cannot Be Monitored (Error Dialog On Startup)
Description: An error dialog saying that local applications cannot be monitored is shown >immediately after VisualVM startup. Locally running Java applications are displayed as Application> (pid ###).
Resolution: This can happen on Windows systems if the username contains capitalized letters. In >this case, username is UserName but the jvmstat directory created by JDK is >%TMP%\hsperfdata_username. To workaround the problem, exit all Java applications, delete the >%TMP%\hsperfdata_username directory and create new %TMP%\hsperfdata_UserName directory.
However, on my machine directory had the following format:
%TMP%\hsperfdata_User.Name
So, my recommendations are:
-check the name of the above mentioned directory for presence of camel case user name
-If not, follow the steps from the Trouble Shooting guide
-If the problem persists, delete the directory (may require machine reboot, as in my case)
-Restart JConsole/JVisualVM
Hopefully, the problem would be resolved.
Thanks and Regards

javax.mail.MessagingException: Could not connect to SMTP host locally via netbeans

I know that there are many questions on this topic, and I have been searching for an answer for the past 4 months. Everyone says check host address, port, and firewall. Well I have done these items, but am still not having any success.
We are running our web application locally using Apache Tomcat 7.0.27 through Netbeans 7.2.1 and are no longer able to connect to the SMTP server to send emails. When running the application on a virtual machine located on the server, there is no issue connecting. We have no problem connecting and sending mail using telnet locally with the same parameters.
We have tried looking at the SMTP logs on the server, and were able to access some logs, but can't find any related to the refused connection. Which SMTP logs would provide more information on this issue? I tend to think that since the connection is refused it may not even trigger any logging, is this a correct assumption? We migrated to IIS7 several months ago and were having trouble accessing IIS 6.0 Manager, so I am not sure that they are even set up correctly.
Does anyone have any ideas of how to further troubleshoot the connection?
Thank you in advance, and please let me know if I can provide any further information.
Almost certainly this is a problem with a firewall or anti-virus program on your local machine. If you can connect from that machine using telnet but can't connect from that same machine using a Java application, there's something on that machine preventing Java applications from connecting.
I recently faced the same problem while running the Mail sending code and what I found is that the code which I had written recently is using the Java version Jre7. But the older codes run in Jre 6 environment, which works perfectly even now also.
So what I had done is I just change the Jre version of my recent code to Jre 6. After that the code works perfectly without any exception.
So try to change your runtime environment to lower Jre and run the code.

How do I debug Java servlet filters, on-the-fly, from a remote machine (I'm using Eclipse and Apache Tomcat 5.5)

I wrote a Java servlet filter on my local machine and deployed it a remote (machine) web server. Unfortunately, it's been very difficult and time-consuming trying to trace errors reported by Apache Tomcat 5.5, my JSP/servlet engine. I can't keep writing System.out.println(...), saving, deploying, testing JSP pages, and so on. It's taking too long. There has to be a better, faster way.
Is it possible to remotely debug servlet filters? I don't have a web server on my local machine, which is why I'm asking about remote debugging. Specifically, I'm looking for a way to debug, line-by-line, the servlet filter, on-the-fly, as it's happening on the remote web server. Does such a method exist?
Or, is there a better method than writing to standard output. It's taking too long and I feel that must be a more efficient means of debugging Java servlet filters.
Note: I'm using Eclipse for development.
Thank you very much for any help.
Update
Thank you all for your help. I really appreciate it.
I added the JVM argument to Tomcat, restarted Tomcat. Then, on the machine with Eclipse, I entered in the appropriate info in the Debug config, put the breakpoint in, and tested. Unfortunately, it did not work. In the config, I left it as Socket Attach, clicked apply, and that was it. I pressed the debug button and it said the connection was refused. I tried ports 8000 and 8001 and both did not work.
Let me explain what I'm trying to do, that might be better.
I have a login page called login.jsp. On that page, is a form whose action attribute is servlet/LoginServlet. When the user submits the form, it calls servlet/LoginServlet, which is mapped to a class in the web.xml file. Let's call this class com.mysite.mypkg.classA. In class A, it calls a method from another class called com.custom.mypkg.classB. I want to put a breakpoint in classB.
So, using the url with login.jsp page in the Eclipse debugger won't call it. I tried using servlet/LoginServlet and that also did not work.
What should I put in for the URL? Or, do I debug this type of setup?
Thank you.
Update 2
I found this site here, which is pretty comprehensive. I ran netstat -a and noticed that the debug port is not listed. Windows Firewall is turned off, but there could be another thing blocking the port, who knows. Anyway, I placed the VM argument here and it's not working.
Thank you.
For remote debugging you need to start the server in debug mode. There are couple of ways doing that.
1 > start the server using
catinlina.bat jpda start
2 > Add an jvm argument to the tomcat java process
-Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n
Once the server is started in debug mode , you need to change the perspective of the project in eclipse to debug.
Then go to Run - > Debug configuration.
Double click remote java application and enter the details such as
Remote IP address
Debug port . Default tomcat debug port is 8000. If you use jvm argument, use the port mentioned in the jvm argument.
Click Apply
Go to the java file you want to debug.
Put a break point in the source code and run the scenario you want to test (Eg Web application using browser)
Also , ensure that the code in the java file is in sync with code deployed on remote server.
Happy Debugging!!!
Peace.
Sanket Raut
You can attach a debugger to a running Tomcat instance, provided that you gave it the right command line options when you launched it.
The Tomcat Development Wiki explains how to do this, and as a bonus gives you instructions on how to set up to debug from the Eclipse or NetBeans IDEs.
Of course, attaching a debugger to a running Tomcat has both security and performance implications*.
* And OH&S issues - you might get badly scratched if you tried this on the wrong kind of tomcat ...
You should run your remote tomcat with the following starup parameter:
bin/catalina.bat jpda start
Then in Eclipse on your local machine go to Run -> Debug Configurations -> Remote Java Application, create new configuration here, use IP of the remote machine as a host and 8000 as a port there
Run this configuration and use the breakpoints in Eclipse for debugging

Debugging Java process using IntelliJ - connects to socket but not to target VM

Now resolved - see end of question.
I am attempting to debug a Java process with IntelliJ Community Edition's debugger. The socket is listening - but when I try to connect the debug process shows the following
'Connecting to the target VM, address: ':8003', transport: 'socket'
It never makes the connection to the VM and I cannot debug.
I have moved to a Windows 7 64 bit PC - on my old XP machine and could connect and debug this Java process (it's an app I build and maintain).
It does not fail with the standard "Connection refused: Connect" error that you would get if no process was there to connect to. Netstat also shows the port is listening when the app is running.
TCP 0.0.0.0:8003 :0 LISTENING
Debug args for the app
-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8003
-Xdebug
If I attempt to run the debug session from IntelliJ on a remote machine, I can connect to the process running on my local machine, and debug.
If I run the Java app on a remote machine, and use IntelliJ on my local machine, I can debug.
It is only if I run and debug on the same machine that it fails - unfortunately this is what I need to do almost all the time.
The only similar issues I have found are from 2004, to do with file paths with spaces, and were NetBeans, not IntelliJ. I have rebuilt and rerun my app ensuring no spaces or underscores in the path, no joy.
My intelliJ debug settings are to debug my local machine on socket, attach mode port 8003 - sorry as a new user I can't attach an image.
Other things I have attempted:
Changing JRE versions
Debugging using shared memory rather than socket transport
I have not re-installed IntelliJ - I am using the same build as my fellow devs (10.5 IC 107-105) who don't have this problem.
Changing the port the debugging process listens on (from 8003 to various other, unused according to netstat)
I have tried using PC name, IP Address and 'localhost' to refer to the PC in the debug settings.
Stuck. Any help much appreciated.
Thanks
Steve
Resolved
Well, a day of failing to solve then I find the answer 20 mins after posting. An invalid JNI signature character, only picked up when debugging. Solved by adding this arg when debugging.
-Dcom.sun.xml.bind.v2.bytecode.ClassTailor.noOptimize=true
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6547438
Thanks for the assistance. Still don't know why it would work when debugging remotely but not locally before.
It sounds like a strange firewall issue.
It appears that you should be able to try telnet localhost 8003 and it should fail. This means nothing can connect to your app on that port.
EDIT: If shared memory does work either and you cannot connecft it via telnet, there is a problem with the way you are running the app.
You will get this type of error if the application is not running.
Can you try debugging a simple program you lauch from IntelliJ?

Categories