I am following the MasteringEJB4thEdition book, which I downloaded from The Server Site website.
There is a simple example of HelloBean, which works perfectly with GlassFish V3 app server. The same example when deployed on JBOSS fails because of JNDI name lookup.
Is there any rule how the JNDI lookup names in JBOSS are decided if we don't provide any ? I found while googling that it is "ear-file-name/Bean-class-name/remote" but it doesn't work for me.
Here is the bean
1. package com.hardik.stateless;
2. import javax.ejb.Stateless;
3. import javax.ejb.Remote
4.
5.
6.
7.
8. #Stateless
9. #Remote(Hello.class)
10. public class HelloBean implements Hello {
11.
12. public String hello() {
13. System.out.println("hello()");
14. return "Hello, World!";
15. }
16.
17. }
Here is the client I am using:
1. package com.hardik.stateless;
2.
3. import javax.naming.Context;
4. import javax.naming.InitialContext;
5.
6.
7. /**
8. * This is an example of client code which invokes
9. * methods on a simple, remote stateless session bean
10. * #author hardik
11. *
12. */
13. public class HelloClient {
14.
15. public static void main(String[] args) throws Exception {
16.
17. Context ctx = new InitialContext();
18. // works for Glassfish
19. //Hello hello = (Hello) ctx.lookup("com.hardik.stateless.Hello");
20. // doesn't work for JBOSS
21. Hello hello = (Hello) ctx.lookup("hello-bean/HelloBean/remote");
22.
23. System.out.println(hello.hello());
24. }
25.
26. }
Here is the error I get while executing the client
# $ wsrunclient.sh -Djava.naming.factory.initial=org.jnp.interfaces.NamingContextFactory -Djava.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces -Djava.naming.provider.url=jnp://localhost:1099 -cp "lib/hello-bean.jar:dist/hello-client.jar:/home/hardik/apps/jboss/client/*" com.hardik.stateless.HelloClient
# log4j:WARN No appenders could be found for logger (org.jnp.interfaces.TimedSocketFactory).
# log4j:WARN Please initialize the log4j system properly.
# Exception in thread "main" javax.naming.NameNotFoundException: hello-bean not bound
# at org.jnp.server.NamingServer.getBinding(NamingServer.java:771)
# at org.jnp.server.NamingServer.getBinding(NamingServer.java:779)
# at org.jnp.server.NamingServer.getObject(NamingServer.java:785)
# at org.jnp.server.NamingServer.lookup(NamingServer.java:396)
# at sun.reflect.GeneratedMethodAccessor260.invoke(Unknown Source)
# at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
# at java.lang.reflect.Method.invoke(Method.java:597)
# at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:305)
# at sun.rmi.transport.Transport$1.run(Transport.java:159)
# at java.security.AccessController.doPrivileged(Native Method)
# at sun.rmi.transport.Transport.serviceCall(Transport.java:155)
# at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:535)
# at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:790)
# at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:649)
# at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
# at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
# at java.lang.Thread.run(Thread.java:619)
# at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:255)
# at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:233)
# at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:142)
# at org.jnp.server.NamingServer_Stub.lookup(Unknown Source)
# at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:722)
# at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:682)
# at javax.naming.InitialContext.lookup(InitialContext.java:392)
# at com.hardik.stateless.HelloClient.main(Unknown Source)
I fixed the problem after searching. I had to add namespace information to my ejb-jar.xml file.
I changed it
from:
<ejb-jar>
<enterprise-beans>
</enterprise-beans>
</ejb-jar>
to:
1. <?xml version="1.0" encoding="UTF-8"?>
2. <ejb-jar version="3.0"
3. xmlns="http://java.sun.com/xml/ns/javaee"
4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
6. http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd">
7. </ejb-jar>
I found the answer here:
http://www.jboss.org/index.html?module=bb&op=viewtopic&t=157022
i think you must be add Following Properties in Client/log4j.properties :
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p
%c{1}:%L - %m%n
log4j.rootLogger=INFO, stdout
I'm not sure about JBoss 5, but under JBoss 4, the JMX console has an MBean called JNDIView, which has operations to dump the entire JNDI tree to the console. If that's still there in JBoss 5, you should be able to find your EJB on there.
Related
Environment Configure
jdk9
mybatis v3.5.4
MacOS
Description
When I was studying Mybatis, I encountered confusing questions. The configuration in question is as follows:
<configuration>
<typeAliases>
<package name="com.xxx.entity" />
</typeAliases>
</configuration>
The code to start mybatis is as follows:
// the var of CONFIG_LOCATION is the url for mybatis configure
Reader config = Resources.getResourceAsStream(CONFIG_LOCATION);
SqlSessionFactory sqlSessioinFactory = new SqlSessionFactoryBuilder().build(config);
When parsing the typeAliases tag, if the subtag is package, an exception will be thrown:
Error building SqlSession.
The error may exist in SQL Mapper Configuration
Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.nio.file.InvalidPathException: Nul character not allowed: com/akamonk/ruixun/entity/Cart.class/����5+ " # $ %
at org.mybatis#3.5.4/org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
at org.mybatis#3.5.4/org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:80)
at org.mybatis#3.5.4/org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:64)
at s2.project/com.akamonk.ruixun.listener.ContextLoader.initContext(ContextLoader.java:39)
at s2.project/com.akamonk.ruixun.listener.ContextLoader.main(ContextLoader.java:59)
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.nio.file.InvalidPathException: Nul character not allowed: com/akamonk/ruixun/entity/Cart.class/����5+ " # $ %
at org.mybatis#3.5.4/org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:121)
at org.mybatis#3.5.4/org.apache.ibatis.builder.xml.XMLConfigBuilder.parse(XMLConfigBuilder.java:98)
at org.mybatis#3.5.4/org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:78)
... 3 more
Caused by: java.nio.file.InvalidPathException: Nul character not allowed: com/akamonk/ruixun/entity/Cart.class/����5+ " # $ %
at java.base/sun.nio.fs.UnixPath.checkNotNul(UnixPath.java:91)
at java.base/sun.nio.fs.UnixPath.normalizeAndCheck(UnixPath.java:81)
at java.base/sun.nio.fs.UnixPath.<init>(UnixPath.java:69)
at java.base/sun.nio.fs.UnixFileSystem.getPath(UnixFileSystem.java:280)
at java.base/jdk.internal.module.Resources.toSafeFilePath(Resources.java:138)
at java.base/jdk.internal.module.Resources.toFilePath(Resources.java:97)
at java.base/jdk.internal.module.ModuleReferences$ExplodedModuleReader.find(ModuleReferences.java:384)
at java.base/jdk.internal.loader.BuiltinClassLoader$2.run(BuiltinClassLoader.java:408)
at java.base/jdk.internal.loader.BuiltinClassLoader$2.run(BuiltinClassLoader.java:403)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.base/jdk.internal.loader.BuiltinClassLoader.findMiscResource(BuiltinClassLoader.java:402)
at java.base/jdk.internal.loader.BuiltinClassLoader.findResources(BuiltinClassLoader.java:333)
at java.base/java.lang.ClassLoader.getResources(ClassLoader.java:1457)
at org.mybatis#3.5.4/org.apache.ibatis.io.VFS.getResources(VFS.java:171)
at org.mybatis#3.5.4/org.apache.ibatis.io.DefaultVFS.list(DefaultVFS.java:105)
at org.mybatis#3.5.4/org.apache.ibatis.io.DefaultVFS.list(DefaultVFS.java:153)
at org.mybatis#3.5.4/org.apache.ibatis.io.VFS.list(VFS.java:200)
at org.mybatis#3.5.4/org.apache.ibatis.io.ResolverUtil.find(ResolverUtil.java:220)
at org.mybatis#3.5.4/org.apache.ibatis.type.TypeAliasRegistry.registerAliases(TypeAliasRegistry.java:130)
at org.mybatis#3.5.4/org.apache.ibatis.type.TypeAliasRegistry.registerAliases(TypeAliasRegistry.java:125)
at org.mybatis#3.5.4/org.apache.ibatis.builder.xml.XMLConfigBuilder.typeAliasesElement(XMLConfigBuilder.java:164)
at org.mybatis#3.5.4/org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:109)
... 5 more
At this time, the content of module-info.java is as follows
open module mybatis.test {
requires org.mybatis;
requires java.sql;
}
There is a strange phenomenon, when I remove the module-info.java from the project, mybatis will not have problems when parsing typeAliases.Therefore, my bold guess is caused by JDK9. But I don't know how to solve it and the specific cause of the problem
I am trying to inject a custom handler to jetty.
I have written the handler in my application code which is packaged as a war.
package com.foo.bar
import javax.servlet.http.{HttpServletRequest, HttpServletResponse}
import org.eclipse.jetty.server.Request
import org.eclipse.jetty.server.handler.AbstractHandler
// scalastyle:off println
class CustomJettyHandler extends AbstractHandler {
override def handle(target: String, baseRequest: Request,
request: HttpServletRequest, response: HttpServletResponse): Unit = {
println("This is a custom jetty handler")
}
}
// scalastyle:on println
I have then injected this handler in the jetty.xml file:
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_3.dtd">
<Configure id="Server" class="org.eclipse.jetty.server.Server">
........
........
<Call name="insertHandler">
<Arg>
<New id="CustomJettyHandler" class="com.foo.bar.CustomJettyHandler"/>
</Arg>
</Call>
........
I am now running jetty in standalone mode. Note that I am passing the location where CustomJettyHandler.class resides to jetty-start.jar.
java -server -Dorg.eclipse.jetty.util.log.class=org.eclipse.jetty.util.log.StdErrLog -jar lib/jetty-start.jar OPTIONS=All --lib=lib/* --lib=webapps/root/WEB-INF/classes/com/foo/bar/* etc/jetty.xml etc/jetty-jmx.xml --debug
In my application logs, I can see jetty loading my custom handler to its classpath but then eventually failing because of ClassNotFoundException. Can someone point out where could this be going wrong?
.......
rawlibref = webapps/root/WEB-INF/classes/com/foo/bar/*
expanded = webapps/root/WEB-INF/classes/com/foo/bar/*
getPaths('webapps/root/WEB-INF/classes/com/foo/bar/*')
Using relative path pattern: glob:**/webapps/root/WEB-INF/classes/com/foo/bar/*
Found [webapps/root/WEB-INF/classes/com/foo/bar/CustomJettyHandler.class] /Users/...path_to_application.../webapps/root/WEB-INF/classes/com/foo/bar/CustomJettyHandler.class
Adding classpath component: /Users/...path_to_application.../webapps/root/WEB-INF/classes/com/foo/bar/CustomJettyHandler.class
.......
.......
.......
URLClassLoader.url[33] = file:/Users/...path_to_application.../webapps/root/WEB-INF/classes/com/foo/bar/CustomJettyHandler.class
Loaded 34 URLs into URLClassLoader
class org.eclipse.jetty.xml.XmlConfiguration - 9.4.24.v20191120
Command Line Args: /var/folders/z5/dmt38gq54kxcrrzpgvbl5m_c0000gp/T/start_6046998329479549547.properties /Users/...path_to_application.../etc/jetty.xml /Users/...path_to_application.../etc/jetty-jmx.xml
2020-05-27 14:46:13.676:INFO::main: Logging initialized #477ms to org.eclipse.jetty.util.log.StdErrLog
2020-05-27 14:46:13.934:INFO:oeju.TypeUtil:main: JVM Runtime does not support Modules
2020-05-27 14:46:14.007:WARN:oejx.XmlConfiguration:main: Config error at <Call name="insertHandler"><Arg>| <New id="CustomJettyHandler" class="com.foo.bar.CustomJettyHandler"/>| </Arg></Call> java.lang.ClassNotFoundException: com.foo.bar.CustomJettyHandler in file:///Users/...path_to_application.../etc/jetty.xml
2020-05-27 14:46:14.007:WARN:oejx.XmlConfiguration:main:
java.security.PrivilegedActionException: java.lang.ClassNotFoundException: com.foo.bar.CustomJettyHandler
at java.security.AccessController.doPrivileged(Native Method)
at org.eclipse.jetty.xml.XmlConfiguration.main(XmlConfiguration.java:1837)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.eclipse.jetty.start.Main.invokeMain(Main.java:218)
at org.eclipse.jetty.start.Main.start(Main.java:491)
at org.eclipse.jetty.start.Main.main(Main.java:77)
Caused by:
java.lang.ClassNotFoundException: com.foo.bar.CustomJettyHandler
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at org.eclipse.jetty.util.Loader.loadClass(Loader.java:64)
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.newObj(XmlConfiguration.java:1028)
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.itemValue(XmlConfiguration.java:1638)
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.value(XmlConfiguration.java:1539)
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.access$500(XmlConfiguration.java:369)
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration$AttrOrElementNode.getList(XmlConfiguration.java:1768)
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration$AttrOrElementNode.getList(XmlConfiguration.java:1744)
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.call(XmlConfiguration.java:919)
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.configure(XmlConfiguration.java:512)
at org.eclipse.jetty.xml.XmlConfiguration$JettyXmlConfiguration.configure(XmlConfiguration.java:454)
at org.eclipse.jetty.xml.XmlConfiguration.configure(XmlConfiguration.java:354)
at org.eclipse.jetty.xml.XmlConfiguration.lambda$main$0(XmlConfiguration.java:1874)
at java.security.AccessController.doPrivileged(Native Method)
at org.eclipse.jetty.xml.XmlConfiguration.main(XmlConfiguration.java:1837)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.eclipse.jetty.start.Main.invokeMain(Main.java:218)
at org.eclipse.jetty.start.Main.start(Main.java:491)
at org.eclipse.jetty.start.Main.main(Main.java:77)
Where did you get this command line from?
java -server \
-Dorg.eclipse.jetty.util.log.class=org.eclipse.jetty.util.log.StdErrLog\
-jar lib/jetty-start.jar\
OPTIONS=All \
--lib=lib/* \
--lib=webapps/root/WEB-INF/classes/com/foo/bar/* \
etc/jetty.xml \
etc/jetty-jmx.xml \
--debug
That's not valid for use with Jetty 9.x's start.jar
Some advice
don't use XML directly on the java command line, that's the responsibility of the start.jar and the jetty-home module system (order is SUPER IMPORTANT).
Your choices of etc/jetty.xml and etc/jetty-jmx.xml is an incomplete list of xmls. (you are missing all dependent XML files)
don't edit the standard Jetty XML files, leave them be, otherwise you'll complicate upgrades later. Use XML to inject your behavior instead (see below for example)
OPTIONS is not supported by Jetty 9.x (that's old school Codehaus / Jetty 6 behavior)
Your usage of --lib= is discouraged, it only supports fully qualified paths to jars or directories with exploded class trees (not relative paths, no globs supported).
-Dorg.eclipse.jetty.util.log.class=org.eclipse.jetty.util.log.StdErrLog is a harsh way of setting up logging. Create a jetty-logging.properties files and make sure it's present on the classpath.
Example contents of jetty-logging.properties
org.eclipse.jetty.util.log.class=org.eclipse.jetty.util.log.StdErrLog
org.eclipse.jetty.LEVEL=INFO
#org.eclipse.jetty.deploy.LEVEL=DEBUG
Do this instead.
Create an injection based XML for your new handler.
my-handler.xml
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_3.dtd">
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<Call name="insertHandler">
<Arg>
<New id="CustomJettyHandler" class="com.foo.bar.CustomJettyHandler" />
</Arg>
</Call>
</Configure>
Next create your jetty-base directory properly (for jetty's start.jar)
# Create your "jetty-base" directory
$ mkdir /path/to/myjettybase
$ cd /path/to/myjettybase
# Establish the basic files / directories / modules that you want to use
# You can find the configuration in start.ini or start.d/*.ini
$ java -jar /path/to/jetty-home/start.jar --add-to-start=http,jmx,deploy,ext,resources
# Copy your custom handler JAR into place
$ cp /path/to/my-handlers.jar /path/to/myjettybase/lib/ext/
# Copy your custom handler XML into place
$ cp /path/to/my-handler.xml /path/to/myjettybase/etc/
# Ensure that the custom handler XML is loaded into the jetty instance at the right point in the XML load order by declaring it to be used in a custom INI
$ mkdir start.d
$ echo "etc/my-handler.xml" >> start.d/my-handlers.ini
# Copy your jetty-logging.properties into place
$ cp /path/to/my-jetty-logging.properties /path/to/myjettybase/resources/jetty-logging.properties
# verify that your configuration looks sane (including the server classpath)
$ cd /path/to/myjettybase
$ java -jar /path/to/jetty-home/start.jar --list-config
# run your instance
$ cd /path/to/myjettybase
$ java -jar /path/to/jetty-home/start.jar
But that's not all, seeing as you seem to want to use jetty-home from a maven-style project (or project layout), you can do that too!
An example project showing this can be found at ...
https://github.com/jetty-project/servlet-error-page-handling
That maven project is also a valid jetty-base directory suitable for execution by a jetty-home archive somewhere else on your machine.
I have a job definition in a java configuration file. When I try and run the job from gradle task, I get IOException parsing XML Document from class path. How do I run a job using CommandLineJobRunner without XML configuration?
Gradle
task executeJob(type: JavaExec) {
main = 'org.springframework.batch.core.launch.support.CommandLineJobRunner'
classpath = sourceSets.test.runtimeClasspath
args = ["--job_path", "C:\\dev\\git\\ncf-bulk-order\\src\\main\\java\\com\\shelter\\NCFBulkOrder\\Jobs\\NCFBulkOrderConfig.java",
"--job_id", "Response",
"--next",
"--customParam", "value"]
}
StackTrace
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [--job_path]; nested exception is java.io.FileNotFoundException: class path resource [--job_path] cannot be opened because it does not exist
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:344)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:181)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:217)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:252)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:613)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:514)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at org.springframework.batch.core.launch.support.CommandLineJobRunner.start(CommandLineJobRunner.java:290)
at org.springframework.batch.core.launch.support.CommandLineJobRunner.main(CommandLineJobRunner.java:565)
Caused by: java.io.FileNotFoundException: class path resource [--job_path] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:330)
... 14 common frames omitted
The CommandLineJobRunner expects either the XML file or the Java configuration class containing the Spring application context where your job is defined. Here is an example with a Java config class:
java -cp your/class/path \
org.springframework.batch.core.launch.support.CommandLineJobRunner \
com.example.MyJobConfiguration \
myJob
So in your command, you don't need to pass the --job_path flag.
Please help me with this issue that is recurring every time I run my code.
I have extracted Geckodriver files in C Drive but when I run my code, the error that comes up is 'Exception in thread "main" java.lang.IllegalStateException: The driver executable does not exist: C:\geckodriver.exe'.
My code is given below:
package Basics;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Browserinvocation {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.gecko.driver","C:\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();//FirefoxDriver class is used to implement methods present in Webdriver-Invocation of browser
driver.get("https://www.amazon.in/");// Get method to hit the url in browser
}
}
Error in console :
Exception in thread "main" java.lang.IllegalStateException: The driver
executable does not exist: C:\geckodriver.exe at
com.google.common.base.Preconditions.checkState(Preconditions.java:534)
at
org.openqa.selenium.remote.service.DriverService.checkExecutable(DriverService.java:136)
at
org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:131)
at
org.openqa.selenium.firefox.GeckoDriverService.access$100(GeckoDriverService.java:41)
at
org.openqa.selenium.firefox.GeckoDriverService$Builder.findDefaultExecutable(GeckoDriverService.java:141)
at
org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:339)
at
org.openqa.selenium.firefox.FirefoxDriver.toExecutor(FirefoxDriver.java:158)
at
org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:120)
at
org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:98)
at Basics.Browserinvocation.main(Browserinvocation.java:13)
Above exception occurs whenever Precondition does not find path of relevant driver mentioned in System.setProperty() method by any reason like below:
if path mentioned have different/wrong/single slashes.
Driver file itself is not present at mentioned location.
If path is mentioned in properties file or config file with double quotes.
Just check once before execution.
You should add the path to geckodriver.exe using / rather than \\. Change your line
System.setProperty("webdriver.gecko.driver","C:\\geckodriver.exe");
to the following
System.setProperty("webdriver.gecko.driver","C:/geckodriver.exe");
Your code is running at my side, might be you are not extracting the gecko driver.
Change the path and try it once, it should worked
Please let me know selenium jars version and your firefox browser version
System.setProperty("webdriver.gecko.driver", "C:/Users/sankalp.gupta/Desktop/JAVASEL/geckodriver.exe");
WebDriver driver=new FirefoxDriver();
driver.get("https://www.amazon.in");
System.out.println(driver.getCurrentUrl());
driver.close();
System.setProperty("webdriver.gecko.driver","C:\\geckodriver.exe");
Here remove . in between gecko and driver
Just download geckodriver.exe and move it to drive C:
I am working on an application that executes a Jython 2.5.3 script from JAVA 1.6.027. The script just open a file using codecs library and it looks like this:
try:
from codecs import open as codecs_open
except ImportError:
print 'ERROR', 'Could not import.'
CODECS_LIST = ['latin-1', 'utf-8', 'utf-16', '1250', '1252']
def open_file(filename, mode):
'''
DOC
'''
for encoding in CODECS_LIST:
try:
f = codecs_open(filename, mode, encoding)
f.read()
f.close()
print 'INFO', "File %s supports encoding %s." % (filename.split("\\")[-1], encoding)
...
except:
...
When I execute this script debugging in Eclipse, everything works OK, but when I execute the part of the JAVA application that invokes this script, I get this error:
Exception in thread "main" java.lang.NoClassDefFoundError: Could not initialize class sun.nio.ch.FileChannelImpl
at java.io.RandomAccessFile.getChannel(RandomAccessFile.java:253)
at org.python.core.io.FileIO.fromRandomAccessFile(FileIO.java:173)
at org.python.core.io.FileIO.<init>(FileIO.java:79)
at org.python.core.io.FileIO.<init>(FileIO.java:57)
at org.python.core.PyFile.<init>(PyFile.java:135)
at org.python.core.PyTraceback.getLine(PyTraceback.java:65)
at org.python.core.PyTraceback.tracebackInfo(PyTraceback.java:38)
at org.python.core.PyTraceback.dumpStack(PyTraceback.java:109)
at org.python.core.PyTraceback.dumpStack(PyTraceback.java:120)
at org.python.core.Py.displayException(Py.java:1080)
at org.python.core.PySystemState.excepthook(PySystemState.java:1242)
at org.python.core.PySystemStateFunctions.__call__(PySystemState.java:1421)
at org.python.core.Py.printException(Py.java:1053)
at org.python.core.Py.printException(Py.java:1012)
at org.python.util.jython.run(jython.java:264)
at org.python.util.jython.main(jython.java:129)
The JAVA application is able to execute others similar jython scripts. I have detected that the class sun.nio.ch.FileChannelImpl is in the library rt.jar, which is inside /bin/common/ folder and included in my classpath via jvm.cfg file:
...
#LIBRARY PATH
./bin/common;...
...
The same way I do it with other libraries that work fine.
I have been stacked with this problem for a few days, so any help will be appreciated.
Thank you
The problem has been resolved by reinstalling Java Runtime Environment, in my case version jre-6u45
This happened to me because the mysql package that I had installed was installed globally and required root privileges, so when running java, I had to include sudo to get it to work correctly.