Jsoup connection appears does not work and does not time out - java

I am working with the following line of code on a project:
Document d = Jsoup.connect(url).timeout(3000).get();
I'm using an IntelliJ debugger and when I get to this line of code, the debugger disables all of it's buttons and just says "the application is running." It does not timeout and it never throws an exception or continue. Does anyone know why this might be happening? If I take the following code
String url = "https://www.cs.purdue.edu/homes/bxd/";
Document d = Jsoup.connect(url).timeout(3000).get();
System.out.println(d);
and run it in the main method of a seperate class it works just fine.

Related

How to log executed lines of code and variables at that point in Java?

I'm working on a project for my Uni where I want to visualize code debugging. For this I somehow need to log the executed Lines of Code and the variables with their values for a given Java program. An example:
public class Main {
public static void main(String[] args){
String abc = "def";
String test = "hello world";
String foo = abc+test;
}
}
If i log this programm my output should be something like this:
Main at line 3:
Main at line 4: abc=def
Main at line 5: abc=def,test = hello world
Main at line 6: abc=def, test = hello world, foo = defhello world
The logging program should run in the background so I can use the logged program normally.
I already tried stuff with Java Agents and Stacktrace but I could'nt get good results. I hope there is any way to do this. Thanks for any help in advance!
There are ways to do this, some IDE like Intelij IDEA actually display the variable value in the editor when you debug.
But if you want to log that, not only the information log would soon become huge (gigabytes/terabytes for real programs) but it would be quite complex.
Here several ways to do this:
Actually use the debugger API to interract with the running program and so log that information: https://docs.oracle.com/javase/8/docs/technotes/guides/jpda/index.html
Make it with a plugin to the compiler so it add the necessary logs. I think that was your approach.
Create a Java => Java compiler that add the matching source and let the standard compiler compile the java. For that there an open source API for eclipse I think that they use for refactoring in the IDE. (Here a blog post that show you can use the API to read java code: https://www.vogella.com/tutorials/EclipseJDT/article.html)

Unable to Get More than 1 Match From String Using Regex

I have been stuck looking around for all sorts of solutions to this problem and I have tried multiple suggestions like this one [1] and have been unsuccessful. The task is relatively simply and I have been successful in grabbing a single match so I know my regex is correct. I am working with Groovy inside of my Jenkins Pipeline script
Here is the problem, I have a file I read from like the following
<log><logentry revision="4813"><msg>nvcs-0909 haha nvpl-6700</msg></logentry></log>
All I want to do is grab the nvcs-0909 and nvpl-6700 and place them as the currentBuild.description. I can do it for one, but can't get multiple entries
Here is what I have so far
textOfFile = readFile("..\\builds\\$BUILD_NUMBER\\changelog0.xml")
regex = /[Nn][Vv]..[-\s]{1,3}\d+/
allIssues = (textOfFile =~ regex)
And when I print allIssues I get
java.util.regex.Matcher[pattern=[Nn][Vv]..[-\s]{1,3}\d+ region=0,377 lastmatch=]
How can I get this to work?
I have also tried
allIssues = textOfFile.findAll(regex)
And that fails as well but
allIssues = textOfFile.find(regex)
works and I get the first match I expected
works fine:
def textOfFile = '''<log><logentry revision="4813"><msg>nvcs-0909 haha nvpl-6700</msg></logentry></log>'''
def regex = /[Nn][Vv]..[-\s]{1,3}\d+/
println (textOfFile =~ regex).findAll()
result:
[nvcs-0909, nvpl-6700]
So the issue was one I faced before but was hidden because I added a try/catch this time around and not earlier with the find method. The current code was
def textOfFile
try {
textOfFile = readFile("..\\builds\\$BUILD_NUMBER\\changelog0.xml")
List<String> allIssues = textOfFile.findAll("[Nn][Vv]..[-\\s]{1,3}\\d+")
issueDescription = ""
for(int i = 0; i < allIssues.size(); i++)
issueDescription += allIssues[i].toUpperCase() + (i < allIssues.size() - 1 ? ", ":"")
currentBuild.description = issueDescription
}
catch(e) {
echo "ERROR"
}
Since I was trying to wrap my head around what in the world was going on I printed the error message with
println e.getMessage()
Inside the catch block and it gave the following message
Scripts not permitted to use staticMethod org.codehaus.groovy.runtime.DefaultGroovyMethods findAll java.lang.String
I ran into this message before and solved it with the help of this. But Jenkins wouldn't give me the option to add permissions and you can't manually add them yourself in (Manage Jenkins -> In-Process Script Approval).
So I commented out the try/catch, ran the script, let Jenkins catch the exception and I was given the option to allow my script to use the findAll method. Then I ran it one more time and it worked perfectly and re-wrapped the code in the try/catch.
I am guessing that the notification mechanism for allowing you to allow method permissions in Jenkins is triggered when your script throws the exception and never catches it..? Anyways it's working perfectly fine now.

NetBeans debugger doesn't stop at breakpoint

I'm running a test case in debug mode on a new installation of Netbeans 8.2 with a break point on a myId field.
#Test
public void testCreateDocumentSecurityNullRequest() throws Exception {
final Integer myId = 1;
myRequest request = null;
mockMvc.perform(post("/pathTo/apply/" + myId).contentType(contentType).content(json(request)))
.andExpect(jsonPath("$.code", is("400")));
}
I know the test is executed because I see the test results, but the debugger doesn't stop on my break point.
The Debugger Console shows:
Listening on 23206
User program running
LineBreakpoint myTest.java : 'line #' successfully submitted.
User program finished
I don't understand why it's not stopping and just continuing? I tried it in Eclipse and it stops and let's me continue as I would expect. I'm sure I'm just misunderstanding something simple but I read documentation and nothing sticks out to me. I also looked at some other Stack Overflow posts that were similar but those didn't seem to fit my issue.
I had a similar issue. NetBeans 6.5 won't stop on a breakpoint in my code.
I figured that while working with a project I changed source location (was: "project.6.5", I changed it to just "project").
NetBeans remembered the old source location ("project.6.5") which became non-matching as I ran code from "project". A breakpoint was displayed correctly (red square), but won't stop.
I opened Window > Debug > Sources and checked current code ("project"). Breakpoint became a broken square and it wrote in logs about source not matching.
After I unchecked old source ("project.5.6") it started working fine again.
...\AppData\Roaming\NetBeans\8.2\config\Services\org-netbeans-modules-debugger-Settings.properties - remove all 'breakpoint' lines from this file.

Java Virtual Machine Launcher Error when presents particular piece of code

I have a code that that reads parameters from XML file. In debugger everything works fine, but after I built JAR file and run it - I get the following window
Java Virtual Machine Launcher
A Java Exception has occured.
But if I comment this piece of code:
else if (settingName.equals("log_level")) {
String value = element.getAttribute("value");
if (value.equals("full")) {
modelLogLevel = EnLogDetails.LOG_FULL;
} else if (value.equals("apdu")) {
modelLogLevel = EnLogDetails.LOG_APDU;
} else if (value.equals("none")) {
modelLogLevel = EnLogDetails.LOG_NONE;
} else {
throw new InvalidArgumentException(new String[]{"log_level"});
}
}
and rebuild JAR again - it works fine. How to fix this issue?
You've provided exactly none of the relevant information, such as the actual exception or the command line you are using, but clearly you are supplying a log_level argument that doesn't match any of those tests, so you are throwing an IllegalArgumentException which is terminating main() and therefore the launcher. If so it is a complete mystery to me why you need to come to StackOverflow to have your own code explained to you.
Or else the references to EnLogDetails are failing in some way which is shown in the exception which you haven't supplied.

A URL specified in a separate line in Java doesn't issue compile-time errors. Why?

Some days age, I mistakenly pasted a URL in my Java program in a hurry, I attempted to run that specific program and in my curiosity, it ran successfully with no warnings, no errors and no exceptions. The similar code is as shown below. I compiles and runs successfully.
final public class Main
{
public static void main(String[] args)
{
System.out.println ("A curiosity");
https://localhost:8181/OnlineShoppingCartSystem-war/Restricted/Home
System.out.println ("compiled Ok!");
}
}
At the second line within the main() method, it should issue some compile-time errors but it works well and displays the respective messages on the console. Why?
The tokens https: gets interpreted as a label. And the // that follows starts a new-line comment. So, the posted code continues to be valid Java (if you notice the syntax coloring).
Because // starts a comment and https: is interpreted as a label.
This is because it looks like this to the compiler:
https: is a code label
//localhost:8181/OnlineShoppingCartSystem-war/Restricted/Home - is a comment
notice that the code highlighter works here as well as in your question.

Categories

Resources