- Complete steps 1-4 of the Eclipse Java IDE tutorial. I assume you will be able to find and install Java and Eclipse (which are installed separately); when you choose a Java or Eclipse download, just choose a "Java" version... there's no need for anything fancy like "Java EE".
- Jump into "Part 2: The Syntax" of Brewing Java: A Tutorial, but stop when you get to "Command Line Arguments".
- In Eclipse, try to run the "Command Line Arguments" example, but realize that you'll get an error.
In order to supply run-time data to your program, you'll have to add an argument to run it: right-click on your program and select "Run As...", but then instead of "Java Application" choose "Run...", then click on the "Arguments" tab and fill in something (like your name) and click "Apply".
Now "Run" your program and you should get some good output. - Now you can continue with Brewing Java: A Tutorial. If you get this far, you should be able to do the rest of any other tutorial out there.
Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts
Thursday, October 15, 2009
Java Tutorial for Absolute Beginners
(I want to give programming newbies an introduction to Java, but in a way that they can do it independently such that the tools won't get in the way (eg. 'javac' to compile, 'java' to run, avoiding classpath issues, and so on). I think the best fit is to have them use Eclipse (or another Java IDE), which will allow them to get to the code quickly; hopefully the IDE won't distract them. Unfortunately, I cannot find any existing tutorials that fit the bill, so here's a set of instructions that I hope will work better.)
Monday, June 22, 2009
Base Test Class for Spring
When using Spring to configure my web application, I usually want to employ the same XML configuration file as the application, especially for database access. So here is what I currently use as the base class for all my integration test cases.
In addition to those two methods, earlier projects have overridden the 'onSetupBeforeTransaction' method as follows, although I found I haven't had to do this for my plain old integration tests:
However, on my Wicket project, I did have to play around with it a bit, so here is what I created for the 'HomePageTest':
One thing you might have noticed is that JMXSpringMBeanNamingStrategy, so I'll explain real quick: it's to allow me to share the same applicationContext.xml file with all my web app contexts such that the JMX engine won't break due to duplicate names. It's inserted this way:
Here is how it's defined; you can tell that it's not ideal, but it's what I got:
public class ServicesTestBase extends AbstractTransactionalDataSourceSpringContextTests {
private static final String CONFIG_LOCATION = "/context/applicationContext.xml";
@Override
protected ConfigurableApplicationContext createApplicationContext(String[] locations) {
ConfigurableWebApplicationContext appContext = new XmlWebApplicationContext();
// this is org.springframework.mock.web.MockServletContext
MockServletContext servletContext = new MockServletContext();
servletContext.addInitParameter(JMXSpringMBeanNamingStrategy.CONFIG_INIT_PARAM_NAME, CONFIG_LOCATION);
appContext.setServletContext(servletContext);
appContext.setConfigLocations(locations);
appContext.refresh();
return appContext;
}
@Override
protected String[] getConfigLocations() {
return new String[]{ CONFIG_LOCATION };
}
}
In addition to those two methods, earlier projects have overridden the 'onSetupBeforeTransaction' method as follows, although I found I haven't had to do this for my plain old integration tests:
@Override
protected void onSetUpBeforeTransaction() throws Exception {
// this is our own home-grown class which you may not need; source may be available by request
StaticApplicationContext.setContext(this.applicationContext); // Do this BEFORE onSetUpBeforeTransaction()
super.onSetUpBeforeTransaction();
}
However, on my Wicket project, I did have to play around with it a bit, so here is what I created for the 'HomePageTest':
@Override
protected void onSetUpBeforeTransaction() throws Exception {
StaticApplicationContext.setContext(this.applicationContext); // Do this BEFORE onSetUpBeforeTransaction()
super.onSetUpBeforeTransaction();
AuthenticatedWebApplication authenticatedWebApp = new BackOfficeApplication() {
@Override
public void init() {
addComponentInstantiationListener(new SpringComponentInjector(this, HomePageTest.this.applicationContext));
initForAppAndAllTests();
}
};
tester = new WicketTester(authenticatedWebApp);
}
One thing you might have noticed is that JMXSpringMBeanNamingStrategy, so I'll explain real quick: it's to allow me to share the same applicationContext.xml file with all my web app contexts such that the JMX engine won't break due to duplicate names. It's inserted this way:
<!-- (Initially copied from http://static.springframework.org/spring/docs/2.0.x/reference/jmx.html ) -->
<!-- This bean must not be lazily initialized if the exporting is to happen. (comment from docs) -->
<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter" lazy-init="false">
<property name="namingStrategy"><bean class="com.max.utils.JMXSpringMBeanNamingStrategy" /></property>
<property name="beans">
<map merge="true">
<entry key="com.max.services.forWebApp:name=data-source" value-ref="dataSource" />
<entry key="com.max.services.forWebApp:name=log4j-settings" value-ref="log4jBean" />
<entry key="com.max.services.forWebApp:name=test-settings" value-ref="dynamicTestSettings" />
</map>
</property>
</bean>
Here is how it's defined; you can tell that it's not ideal, but it's what I got:
public class JMXSpringMBeanNamingStrategy implements ObjectNamingStrategy, ServletContextAware {
public static final String CONFIG_INIT_PARAM_NAME = "contextConfigLocation";
private ServletContext context;
public void setServletContext(ServletContext _context) {
this.context = _context;
}
public ObjectName getObjectName(Object managedBean, String beanKey) throws MalformedObjectNameException {
// There's got to be a better way to get the context name of this app!
String springWebAppConfigFile = context.getInitParameter(CONFIG_INIT_PARAM_NAME);
String configName = springWebAppConfigFile;
configName = configName.replace("classpath", "");
configName = configName.replace("webapp", "");
configName = configName.replace("xml", "");
configName = configName.replace("context", "");
configName = configName.replaceAll("[\\:\\/\\.\\-]", "");
String newBeanKey = beanKey.replaceFirst("forWebApp", configName);
return new ObjectName(newBeanKey);
}
}
Tuesday, May 12, 2009
logging the XML payload with Axis, XFire
When using Axis to send data to a web service, you can set the logger level on org.apache.axis.transport.http.HTTPSender to DEBUG to see what's going out.
When using Xfire to receive data, you can set the logger level on org.apache.axis.transport.http.HTTPSender to DEBUG to see what's coming in.
Here's a sample in log4j.properties:
When using Xfire to receive data, you can set the logger level on org.apache.axis.transport.http.HTTPSender to DEBUG to see what's coming in.
Here's a sample in log4j.properties:
log4j.logger.org.apache.axis.transport.http.HTTPSender=DEBUG
log4j.logger.org.codehaus.xfire = DEBUG
Tuesday, November 18, 2008
a few notes about the Java ClassLoader
I found a case today where the clazz.getResourceAsStream(String) retrieved a resource but the clazz.getClassLoader.getResourceAsStream(String) failed to find it. Weird.
I never figured it out, but I came up with the following as I tried:
What is the difference between the clazz.getResource and clazz.getClassLoader().getResource methods?
I never figured it out, but I came up with the following as I tried:
- There is no way to get all the entire path that is being searched. I always come to this point and try, but it's a hard fact of Java. I did find this blog entry that explained how you might patch the ClassLoader.toString() methods. But read on...
- Even for URLClassLoader classes, it looks like it'll search through directories that are not found by the getURL() method (so that previous blog entry won't catch them all). I found this by running some unit tests in Maven with the method that follows, and it was retrieving things from the 'classes' and 'test-classes' directories that didn't show in the getURL() results.
- If you try a call to getResource(""), it'll show some of the directory paths that are in the search path. I don't know for sure that this is true because I can't find it documented, but it seems to work. So the following method will show all the URL and path resources up the line from a given loader:
private static void showAllResourcePaths(ClassLoader loader) {
try {
String indent = "";
for (ClassLoader nextLoader = loader; nextLoader != null; nextLoader = nextLoader.getParent()) {
System.out.println(indent + " loader " + nextLoader);
for (Enumeration<URL> urls = nextLoader.getResources(""); urls.hasMoreElements(); ) {
URL url = urls.nextElement();
System.out.println(indent + " path: " + url);
}
if (nextLoader instanceof URLClassLoader) {
System.out.println(indent + " URLs: " + Arrays.asList(((URLClassLoader) nextLoader).getURLs()));
}
indent += "-";
}
} catch (Exception e) {
e.printStackTrace();
}
}
- Using that method will show you most of what you want because, in my tests, all the class loaders in the ancestry turned out to be URLClassLoaders.
What is the difference between the clazz.getResource and clazz.getClassLoader().getResource methods?
Thursday, November 6, 2008
Tuesday, October 21, 2008
Google GData APIs
This is just so I can find the Java API for Google GData. (Go ahead: try googling "gdata api" and see if you can find it that way. I dare you.)
http://code.google.com/apis/gdata/javadoc/
Note that it's linked from the "Client Libraries" page.
Update: you can find it easily in a Google search on: "gdata javadoc"
http://code.google.com/apis/gdata/javadoc/
Note that it's linked from the "Client Libraries" page.
Update: you can find it easily in a Google search on: "gdata javadoc"
Subscribe to:
Posts (Atom)