I have a script for Jython that works perfectly, but it开发者_StackOverflow社区's really really slow, so I decided to try to see if I can convert it to pure java and see if it speeds it up.
In Jython I am using:
from java.util import logging
from java import lang
from org.apache.commons.logging import LogFactory
logger = LogFactory.getLog('com.gargoylesoftware.htmlunit')
logger.getLogger().setLevel(logging.Level.OFF)
webclient = WebClient(BrowserVersion.FIREFOX_3_6)
webclient.setThrowExceptionOnFailingStatusCode(False)
This basically prevents all the annoying warning messages from htmlunit to stop coming on the screen (it tends to complain a lot if the code it's reading isn't perfect but still ends up reading it).
In Java I tried to copy and paste the same code, but Java seems to ignore it. If I add types to my import, it doesn't give me an error it just keeps doing the same thing.
import java import.lang;
import org.apache.commons.logging.LogFactory;
LogFactory.getLog('com.gargoylesoftware.htmlunit');
LogFactory.getLogger().setLevel(logging.Level.OFF);
final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3_6);
webClient.setThrowExceptionOnFailingStatusCode(false);
Since the file is fairly large and I'll have to do quite a been of converting of code, what is the logic of what I am doing wrong?
Java's Syntax is not equal to Python's Syntax. imports are declared on the top of the Class-File, other things are done within the "class"-container.
Basically a Java file looks something like this:
import [...]
public class YourClass{
public YourClass(){
// Constructor
}
public static void main(Stirng[] args){
// The method Java calls when a class is executed in a JVM
}
}
You should check out some Java-Tutorials first, because Java and Python are quite different.
精彩评论