I'm setting up a very simple project, just trying to get a basic Hibernate setup without maven. I've downloaded the jar, put it under a /lib directory within my project and added this directory as a library under module settings. I can access Hibernate's classes and compile the project.
However, after declaring that the method throws a HibernateException (that extends PersistenceException), the code doesn't compile:
HibernateStatesMain.java:
package example;
import org.hibernate.HibernateException;
import org.hibernate.cfg.Configuration;
public class HibernateStatesMain {
public static void main(String[] args) throws HibernateException {
Configuration config = new Configuration().configure();
}
}
And when I build, I get:
Error:(11, 24) java: cannot access javax.persistence.PersistenceException
class file for javax.persistence.PersistenceException not found
This should be a very simple thing, but I can't get my head around it. What am I doing wrong?
Related
There are some cases that the software shall behave differently according to some environmental conditions, for example whether a file exists at some place or not.
In my case, I'm developing a library, and it is configured according to a configuration file in classpath, (and falls back to default behavior if the config file does not exists).
How shall I unit test this class?
I need to write tests for evaluating the class in following cases:
the file does not exists on classpath
the file with content A exist on classpath
the file with content B exist on classpath
But I don't know how to configure environment to justify all of them. And execute the test one after each other.
By the way I'm using Java, and I have both JUnit and TestNG on the test classpath.
Edit:
One of the problems is that the config file resides in classpath, so if the normal ClassLoader finds and loads it, it returns the same content as long as the same class loader is used.
And I believe using a custom ClassLoader for testing is so complicated, that it needs tests to validate the tests!
You can use a temporary file created by your test to mock out the path in your class.
ConfigurationTest.java:
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.junit.Assume.assumeThat;
import java.nio.file.Files;
import org.junit.Test;
public class ConfigurationTest {
private Configuration config = new Configuration();
#Test
public void testWithConfigFile() throws Exception {
config.configFile = Files.createTempFile("config_",".ini");
config.configFile.toFile().deleteOnExit();
assertFalse(config.isInDefaultMode());
}
#Test
public void testWithoutConfigFile() throws Exception {
assumeThat(Files.exists(config.configFile), is(false));
assertTrue(config.isInDefaultMode());
}
}
Configuration.java:
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class Configuration {
Path configFile = Paths.get("config.ini");
public boolean isInDefaultMode() {
return !Files.exists(configFile);
}
}
I want to load and execute jar file using custom ClassLoader.
My code:
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
public class Main {
private static JarClassLoader customClassLoader;
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IOException {
customClassLoader = new JarClassLoader(new File(args[0]).toURI().toURL());
customClassLoader.invokeClass(customClassLoader.getMainClassName(), new String[]{});
}
}
Everything works great with simple jars but when I want execute more complicated jars, they not always work properly. For example if program is using apache logging service I will see
"ERROR StatusLogger Unable to locate a logging implementation, using SimpleLogger"
How to get around this.
I'm a newbie to Java and to EJB. Want to create a simple EJB.
So I've created an EJB Project via Eclipse (Eclipse Java EE IDE Juno Service Release 1 Build ID: 20120920-0800). Then I've added a Stateless Session Bean there with Remote and Local interfaces. Then I added a simple method that just returns a sum of 2 numbers. Here is the code:
Test.java:
package ejb;
import javax.ejb.Stateless;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
/**
* Session Bean implementation class Test
*/
#Stateless
#TransactionManagement(TransactionManagementType.BEAN)
public class Test implements TestRemote, TestLocal {
/**
* Default constructor.
*/
public Test() {
// TODO Auto-generated constructor stub
}
public int getSum(int a , int b) {
return (a + b);
}
}
TestLocal.java:
package ejb;
import javax.ejb.Local;
#Local
public interface TestLocal {
public int getSum(int a , int b);
}
TestRemote.java :
package ejb;
import javax.ejb.Remote;
#Remote
public interface TestRemote {
public int getSum(int a , int b);
}
I right-clicked on my EJB Project and exported it as a JAR file. Then I deployed it to GlassFish3 server: 1) Applications - Deploy and so on; 2) JNDI - Custom Resources. I've set Resource Type to ejb.TestRemote , Factory Class to org.glassfish.resources.custom.factory.EjbFactory . JNDI Name was Test. Then I've coded a simple client:
Main.java:
import javax.naming.InitialContext;
import ejb.TestRemote;
public class Main {
public static void main(String arg[]) {
try {
TestRemote testR = (TestRemote) new InitialContext().lookup("Test");
//System.out.println(new InitialContext().lookup("Test").toString());
//.toString displays: Reference Class Name: ejb.TestRemote
System.out.println(testR.getSum(50, 500));
} catch(Exception e) {
e.printStackTrace();
}
}
}
I've added gf-client-module.jar from C:\glassfish3\glassfish\modules and a JAR file of my EJB Project to Java Build Path (Project-Properties).
Then I tried to launch my client in Eclipse and got following message appeared immediately:
com.sun.enterprise.v3.server.CommonClassLoaderServiceImpl findDerbyClient
INFO: Cannot find javadb client jar file, derby jdbc driver will not be available by default.
Then I got next message:
java.lang.ClassCastException: javax.naming.Reference cannot be cast to ejb.TestRemote
at Main.main(Main.java:16)
So, can anyone please help me with that?.. I'm really confused: .toString method called on a reference displays that it is a proper ejb.TestRemote class reference, as I mentioned before. But... It doesn't work ((
Any ideas? Thanks in advance.
RESOLVED FOR ME, BUT WITH SOME ANOTHER WAY
I've lost hope to make it with Eclipse and separately installed GlassFish. So, I've installed a GlassFish server to Eclipse, and deployed my EJB project there. It worked: now my Client works fine. The only change was - I should provide a full package name to .lookup: like .lookup("ejb.TestRemote").
Thanks everyone who tried/wanted/wished to help!
I have built a simple project called LibTest that has one class with the following code:
public class MainTest
{
public static tclass l;
}
In secondary simple project I have the defined class tclass:
public class tclass
{
int i;
}
Then I export tclass to a JAR file. At LibTest->Properties->BuildPath I click on AddExternalJar and select tclass.jar ( I also tried checking the JAR at Order and Export) but I still get an error at MainTest "tclass cannot be resolved to a type".
I don't see what is missing.
Thanks
Simon
After including your jar file, you also need to import your class using its full path in your MainTest class. e.g:
import com.package.tclass;
I am trying to imported a java class from an external lib in jyhon and it does not work. An example
package run;
import import.Imported;
Class Run()
{
public static void main(String[] args){
pi = new PythonInterpreter(null);
pi.execfile('script.py');
}
}
//this is an external libary
package import;
Class Imported()
{
//some stuff;
}
//py script
from import import Imported //this line throws an error Module not found
#do some stuff
The strangest thing is that it runs when it is compiled in Eclipse, but does not from command line.
Any help?
Sounds like your classpath is probably set incorrectly at runtime. The easiest solution is typically just to add the directory or jar file containing 'import' to sys.path.
(Also, naming your packages 'import' is just asking for trouble.)