I´m currentl trying to get into JavaFX 2.0 but I can´t get .css Stylesheets to work within my application. I´ve followed the guide at http://docs.oracle.com/javafx/2/get_started/css.htm#BABBGJBI but whenever I try to import a .css file via
scene.getStylesheets().add(Login.class.getResource("loginform.css").toExternalForm());
I get the following Error:
Exception in Application start method
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:399)
at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:47)
at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:115)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
at Login.start(Login.java:68)
at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:315)
at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:174)
at com.sun.javafx.application.PlatformImpl$3.run(PlatformImpl.java:141)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29)
at com.sun.glass.ui.win.WinApplication$2$1.run(WinApplication.java:62)
... 1 more
The css path is located in my eclipse project path:
C:\Users\UserName\Dropbox\Documents\Eclipse\FirstExamples\loginform.css
I´d appreciate any help, I can´t get it working, not with absolute paths nor with relative ones like shown in the example.
Check the compiled output path of your eclipse project and ensure that loginform.css is in the same directory as Login.class
I´ve solved the problem by putting stylesheets into my src folder not
a very nice solution but going up by ../formm.css doesn´t seem to work
dunno why
You are requesting the css file via the class.getResource method, which will look for the resource in the same location the class is located. When you put the stylesheet in your src folder, then whatever build system you are using is copying the stylesheet to the same directory as Login.class during the build. When you don't place it in that src folder, then the copy is not occuring.
If you don't want to place the css file in the same directory as your Java source, then either set your build system up to copy it across from another directory OR don't use the classloader mechanism when you add a stylesheet, e.g. use an absolute file or http url or a file url based on the user.dir system property (current working directory) instead.
Really though, just placing leaving the stylesheet in the src folder and accessing it in the manner you are doing is fine and it also makes resource location easy when you deploy the app to different environments.
If you use anything as a resource, you have to prefix the name of it with "/" and that resource has to be on the classpath.
I have the setup with a directory e.g. resources, which contains the .css. Then I use this:
scene.getStylesheets().add(Login.class.getResource("/loginform.css").toExternalForm());
The directory 'resources' which contains the .css file is added to the classpath (in Eclipse, for example, just make it be a source folder).
It is however pity that the exception is totally useless; I've noticed that the .css was the problem when I added it for the first time.
Related
I have a question regarding file handling.
I automate a page using selenium, and I need to upload a file in this page.
I want to put the file in resource folder and read it's path in the test (since many OS and path will be different to any computer WIN/MAC).
I put the file manually in the resource folder, and it put it in:
X:\Project_11_01_2021\src\test\resources
when I used the ClassLoader and try to find the file it not found it,
I saw that if I manually put it in this path it find it, found.
X:\Project_11_01_2021\out\test\resources
the problem is that I am using git and if I add to the resources it upload to git and every one will get the change, and when I put in out\test\resources it is not displayed in the source tree to commit to git.
is their a way that classLoader will search in the first location? and not in the second?
[][path that worked]
[][when here not worked]
/******* test *******/
public void entertax() throws Exception {
WebDriver deiver2 = getWebDriver();
Thread.sleep(1000);
ClassLoader classLoader = getClass().getClassLoader();
String path = classLoader.getResource("TAX12.pdf").getPath();
System.out.println("\n\n path is " + path);
deiver2.switchTo()
.activeElement();
deiver2.findElement(By.xpath("//input[#type='file']"))
.sendKeys(
"X:\\Project_11_01_2021\\out\\test\\resources\\fw8TAX12.pdf");
System.out.println("END");
}
This issue looks more like an IDE configuration problem.
Your code classLoader.getResource("TAX12.pdf") looks correct (1. using classLoader.getResource() method && 2. specifying the correct relative path within the resources folder).
I assume why the code fails to find the file is due to the fact that the resources folder is not part of your applications classpath (I'm assuming your trying to run the code from your IDE which apparently seems to be IntelliJ).
I'm not regularly using IntelliJ, but you can specify the classpath settings in the Module settings.
In the Module settings specify the resources folder as a resource:
When successfully added the resources folder to the classpath it should display an icon like this in the project explorer:
After adding the resources folder to your classpath everything should work.
EDIT:
In case you're using Maven also make sure to specify the resources folder as such in the pom.xml file correspondingly.
Also make sure to spell the name of the file you're looking for "TAX12.pdf" correctly, since it seems to differ in your code and in your screenshots (not sure if it differs only because posting it here or also in your real code base).
I am trying to remove the OS-specific chromium binaries to shrink the size of a jar, and install them on first run. I tried to follow the docs, so I removed the -mac, -linux, and -win jxbrowser jars from the pom.xml file, extracted the jxbrowser-mac-6.17.jar into a directory, and set JXBrowser to load the binaries from there, via the following 3 functions (one at a time and combining them):
BrowserPreferences.setChromiumDir(path), System.setProperty("jxbrowser.chromium.dir", path), and System.setProperty(BrowserPreferences.CHROMIUM_DIR_PROPERTY, path)
The specified directory contains the jxbrowser-mac-6.17.jar and its extracted files (7zr-mac, chromium-mac.7z, com and META_INF directories). I am creating a lightweight browser with the following:
BrowserContextParams params = new BrowserContextParams(chromiumDir.getAbsolutePath());
BrowserContext context = new BrowserContext(params);
Browser browser = new Browser(BrowserType.LIGHTWEIGHT, context);
When this code runs, the 7zr-mac and chromium-mac.7z files disappear from the directory, and several errors are thrown, like these:
Caused by: com.teamdev.jxbrowser.chromium.internal.ChromiumExtractorException: Failed to extract Chromium binaries into /Users/bsettle/CytoscapeConfiguration/3/karaf_data/tmp/browsercore-60.0.3112.113.6.17/jxbrowser-mac-6.17
at com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor.extractChromiumBinariesIfNecessary(SourceFile:88)
at com.teamdev.jxbrowser.chromium.internal.ipc.IPC.<init>(SourceFile:66)
at com.teamdev.jxbrowser.chromium.internal.ipc.IPC.create(SourceFile:96)
at com.teamdev.jxbrowser.chromium.internal.ipc.IPC$a.<clinit>(SourceFile:443)
... 7 more
Caused by: java.lang.IllegalArgumentException: The /chromium-mac.7z resource cannot be found in JAR files
at com.teamdev.jxbrowser.chromium.internal.XZExtractor.a(SourceFile:112)
at com.teamdev.jxbrowser.chromium.internal.XZExtractor.a(SourceFile:69)
at com.teamdev.jxbrowser.chromium.internal.XZExtractor.extract(SourceFile:37)
at com.teamdev.jxbrowser.chromium.internal.b.run(SourceFile:1066)
at java.security.AccessController.doPrivileged(Native Method)
at com.teamdev.jxbrowser.chromium.internal.ChromiumExtractor.extractChromiumBinariesIfNecessary(SourceFile:60)
... 10 more
All of the errors state that JxBrowser Failed to extract Chromium binaries because The ___ resource cannot be found in JAR files, but it shouldn't be trying to extract them if they already exist in the Chromium directory? (according to the docs).
If I try to open the browser again (pointing to the directory with the binary files now missing, I get a new error:
Exception in thread "Thread-150" java.lang.NoClassDefFoundError: Could not initialize class com.teamdev.jxbrowser.chromium.BrowserContext
at org.cytoscape.cyndex2.internal.util.BrowserManager.getJXBrowser(BrowserManager.java:72)
at org.cytoscape.cyndex2.internal.util.BrowserManager.getBrowserView(BrowserManager.java:49)
at org.cytoscape.cyndex2.internal.task.LoadBrowserTask$1.run(LoadBrowserTask.java:53)
at java.lang.Thread.run(Thread.java:748)
Even though the base jxbrowser-6.17.jar is still packaged with the app.
I'm not sure what else to try, but I would like to not have to package the OS-specific binaries with the jar because that raises the file size from 8MB to 160MB.
Thanks
Each platform-specific JAR file contains a 7z archive inside with binary files. JxBrowser expects to find the binary files from the corresponding archive in the directory specified by the jxbrowser.chromium.dir System Property and in case they do not exist, tries to extract them. To make it work please extract the binaries from the corresponding archive into the directory specified by the jxbrowser.chromium.dir System Property.
i have created two different java projects on project is depend on other and
like A project and B project A project has logs folder and in log4j.xml i have given relative path of log files
now i am using A project as api in B project and i have created executable jar of B project when i am calling B it is giving me FileNotFoundException.
Please suggest me .............
log4j:ERROR setFile(null,true) call failed.
java.io.FileNotFoundException: ..\Framework\logs\log.log (The system cannot find the path specified)
at java.io.FileOutputStream.openAppend(Native Method)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at org.apache.log4j.FileAppender.setFile(FileAppender.java:273)
at org.apache.log4j.FileAppender.activateOptions(FileAppender.java:152)
at org.apache.log4j.DailyRollingFileAppender.activateOptions(DailyRollingFileAppender.java:206)
at org.apache.log4j.config.PropertySetter.activate(PropertySetter.java:247)
at org.apache.log4j.xml.DOMConfigurator.parseAppender(DOMConfigurator.java:210)
at org.apache.log4j.xml.DOMConfigurator.findAppenderByName(DOMConfigurator.java:140)
at org.apache.log4j.xml.DOMConfigurator.findAppenderByReference(DOMConfigurator.java:153)
at org.apache.log4j.xml.DOMConfigurator.parseChildrenOfLoggerElement(DOMConfigurator.java:415)
at org.apache.log4j.xml.DOMConfigurator.parseRoot(DOMConfigurator.java:384)
at org.apache.log4j.xml.DOMConfigurator.parse(DOMConfigurator.java:783)
at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:666)
at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:616)
at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:602)
at org.apache.log4j.helpers.OptionConverter.selectAndConfigure(OptionConverter.java:460)
at org.apache.log4j.LogManager.<clinit>(LogManager.java:113)
at com.netapp.ca.framework.logger.LogImpl.<init>(LogImpl.java:35)
log4j:ERROR Either File or DatePattern options are not set for appender [allLog].
log4j:ERROR setFile(null,true) call failed.
I think you are executing from a jar, and therefore (of course) the path is wrong because you can't save log files (normally) within a jar.
I've had the same problem and worked it out writing the following method to receive a path where my application can work:
String applicationPath = this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
applicationPath = applicationPath.replace('\\', '/');
if (applicationPath.startsWith("file:")) applicationPath = applicationPath.substring(5);
if (applicationPath.contains(".jar")) {
String[] parts = applicationPath.split("/");
applicationPath = applicationPath.replaceAll(parts[parts.length - 1], "");
}
I don't know how your application is structured but I think this code could help you. Simply write the "applicationPath" to the console and you know what's wrong with the path. I bet it is something like the jar-problem.
It is unclear what you are trying to do, but in simple terms it appears that Log4J cannot find the logfile which you specified in the log4j.xml config.
It is best to use absolute paths when specifying the logging directory for Log4J, but if you insist in using relative paths then make sure you are absolutely sure which directory is the "working directory" for your application.
You can log the working directory for your application with the following code (put this in your Java executable, main method):
System.out.println(System.getProperty("user.dir"));
Once you know the working directory, you can update the logfile path in your log4j.xml config.
I am trying to use ESAPI.jar for providing security to my web application.Basically I have just started using ESAPI.jar.
But problem is I am not able to run even a simple program using ESAPI.
The small code snippet is:
String clean = ESAPI.encoder().canonicalize("someString");
Randomizer r=ESAPI.randomizer();
System.out.println(r);
System.out.println(clean);
I get this error:
Attempting to load ESAPI.properties via file I/O.
Attempting to load ESAPI.properties as resource file via file I/O.
Not found in 'org.owasp.esapi.resources' directory or file not readable: D:\Eclipse-Workspace\Test\ESAPI.properties
Not found in SystemResource Directory/resourceDirectory: .esapi\ESAPI.properties
Not found in 'user.home' (C:\Documents and Settings\user.user) directory: C:\Documents and Settings\user.user\esapi\ESAPI.properties
Loading ESAPI.properties via file I/O failed. Exception was: java.io.FileNotFoundException
Attempting to load ESAPI.properties via the classpath.
ESAPI.properties could not be loaded by any means. Fail. Exception was: java.lang.IllegalArgumentException: Failed to load ESAPI.properties as a classloader resource.
Exception in thread "main" org.owasp.esapi.errors.ConfigurationException: java.lang.reflect.InvocationTargetException SecurityConfiguration class (org.owasp.esapi.reference.DefaultSecurityConfiguration) CTOR threw exception.
at org.owasp.esapi.util.ObjFactory.make(ObjFactory.java:129)
at org.owasp.esapi.ESAPI.securityConfiguration(ESAPI.java:184)
at org.owasp.esapi.ESAPI.encoder(ESAPI.java:99)
at org.rancore.testJasp.TestEsapi.main(TestEsapi.java:59)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.owasp.esapi.util.ObjFactory.make(ObjFactory.java:86)
... 3 more
Caused by: org.owasp.esapi.errors.ConfigurationException: ESAPI.properties could not be loaded by any means. Fail.
at org.owasp.esapi.reference.DefaultSecurityConfiguration.loadConfiguration(DefaultSecurityConfiguration.java:439)
at org.owasp.esapi.reference.DefaultSecurityConfiguration.<init>(DefaultSecurityConfiguration.java:227)
at org.owasp.esapi.reference.DefaultSecurityConfiguration.getInstance(DefaultSecurityConfiguration.java:75)
... 8 more
Caused by: java.lang.IllegalArgumentException: Failed to load ESAPI.properties as a classloader resource.
at org.owasp.esapi.reference.DefaultSecurityConfiguration.loadConfigurationFromClasspath(DefaultSecurityConfiguration.java:667)
at org.owasp.esapi.reference.DefaultSecurityConfiguration.loadConfiguration(DefaultSecurityConfiguration.java:436)
... 10 more
I have tried copying the 3 ESAPI properties files in my source folder and also configuring them on build path but still I have not succeeded. I have tried many permutations and combinations to no avail.
Please guide me.
The content of property file is:
# User Messages
Error.creating.randomizer=Error creating randomizer
This.is.test.message=This {0} is {1} a test {2} message
# Validation Messages
# Log Messages
The ESAPI.properties file should have more than 3 lines in it. See for example:
https://web.archive.org/web/20150904064147/http://code.google.com:80/p/owasp-esapi-java/source/browse/trunk/configuration/esapi/ESAPI.properties
In my experience the ESAPI.properties file either needs to be in the same folder as the esapi.jar or needs to be compiled into the jar in a resources directory.
/resources/ESAPI.properties
I believe that either one should work. If ESAPI does not find the file it one location it looks in others.
The code for that is here around line 620:
https://web.archive.org/web/20161005210258/http://code.google.com/p/owasp-esapi-java/source/browse/trunk/src/main/java/org/owasp/esapi/reference/DefaultSecurityConfiguration.java
I just struggled through this one myself.
I created a folder called esapi in my C:/users/myname/ directory and loaded up the ESAPI.properties, validation.properties, and the ESAPI-AccessControlPolicy.xml which got me past all the not finding files errors. ESAPI looks in several places for the files. I'm running Windows 7 64 bit by the way.
Then I had to update some of the jars. I should have done this from the beginning, but I didn't know it. I was getting this error:
AccessController class (org.owasp.esapi.reference.DefaultAccessController) CTOR threw exception.
for every jar that needed to be a newer version, like commons collections or log4j. At the bottom of the stack trace it would reference the offending jar.
When I added the newer jars from the esapi lib directory everything just worked!
I got this message at the end in my console:
ESAPI.accessController found: org.owasp.esapi.reference.DefaultAccessController#1cb8deef
Note: There is an ESAPI_en_US.properties file, but it's the one with only a few lines in it. Just use the file in: \esapi-2.1.0-dist\src\test\resources\esapi\ESAPI.properties. That is the full complete file.
After having this problem and looking at the installation documentation (esapi-x.x.x-dist\documentation\esapi4java-core-x-x-install-guide.pdf) I found a very useful section which detailed that the properties file can be anywhere, provided a vm flag (-Dorg.owasp.esapi.resources=path") points to a particular directory.
For example, if I stick the file in a "resources" folder at the root of my project directory, then the flag would be:
-Dorg.owasp.esapi.resources="path\to\project\root\resources"
The reference SecurityConfiguration manages all the settings used by the ESAPI in a single place. In this reference implementation, resources can be put in several locations, which are searched in the following order:
1) Inside a directory set with a call to SecurityConfiguration.setResourceDirectory( "C:\temp\resources" ).
2) Inside the System.getProperty( "org.owasp.esapi.resources" ) directory. You can set this on the java command line as follows (for example):
java -Dorg.owasp.esapi.resources="C:\temp\resources"
You may have to add this to the start-up script that starts your web server. For example, for Tomcat, in the "catalina" script that starts Tomcat, you can set the JAVA_OPTS variable to the -D string above.
3) Inside the System.getProperty( "user.home" ) + "/.esapi" directory (supported for backward compatibility) or inside the System.getProperty( "user.home" ) + "/esapi" directory.
4) The first ".esapi" or "esapi" directory on the classpath. (The former for backward compatibility.)
Can you put your file (with this name) in:
D:\Eclipse-Workspace\Test\ESAPI.properties
And show us the contents and exception again.
Extract the esapi jar
create a folder named resources under org.owasp.esapi
copy ESAPI.properties to the org.owasp.esapi.resources
Build and deploy
extract the jar
add properties file under resources folder.
initially getting same error, after updating properties file it worked for me
Thanks for providing the information
-Dorg.owasp.esapi.resources="path\to\project\root\resources"
This is a good source of information and has resolved my issue
I had the same problem too. I resolved it using a little bit of James Drinkard solution. What I basically did is created a new folder with name ESAPI and added ESAPI.properties file, Validation.properties, and the ESAPI-AccessControlPolicy.xml. And archived it into a jar file and added to the lib folder under WebContet/WEB-INF and build it to the path and it worked.
*To archive it into a jar file I used windows command move ESAPI ESAPI.jar
Look at the various scripts under 'src/examples/scripts' and they will show you one easy way to control where the ESAPI.properties files is found. (This is for ESAPI 2.0 or later.)
You will find a copy of the ESAPI.properties file will be under 'configuration/esapi'.
I need to access the resource files in my web project from a class. The problem is that the paths of my development environment are different from the ones when the project is deployed.
For example, if I want to access some css files while developing I can do like this:
File file = new File("src/main/webapp/resources/styles/some.css/");
But this may not work once it's deployed because there's no src or main directories in the target folder. How could I access the files consistently?
You seem to be storing your CSS file in the classpath for some unobvious reason. The folder name src is typical as default name of Eclipse's project source folder. And that it apparently magically works as being a relative path in the File constructor (bad, bad), only confirms that you're running this in the IDE context.
This is indeed not portable.
You should not be using File's constructor. If the resource is in the classpath, you need to get it as resource from the classpath.
InputStream input = getClass().getResourceAsStream("/main/webapp/resources/styles/some.css");
// ...
Assuming that the current class is running in the same context, this will work regardless of the runtime environment.
See also:
getResourceAsStream() vs FileInputStream
Update: ah, the functional requirement is now more clear.
Actually I want to get lastModified from the file. Is it possible with InputStream? –
Use getResource() instead to obtain it as an URL. Then you can open the connection on it and request for the lastModified.
URL url = getClass().getResource("/main/webapp/resources/styles/some.css");
long lastModified = url.openConnection().getLastModified();
// ...
If what you're looking to do is open a file that's within the browser-visible part of the application, I'd suggest using ServletContext.getRealPath(...)
Thus:
File f = new File(this.getServletContext().getRealPath("relative/path/to/your/file"));
Note: if you're not within a servlet, you may have to jump through some additional hoops to get the ServletContext, but it should always be available to you in a web environment. This solution also allows you to put the .css file where the user's browser can see it, whereas putting it under /WEB-INF/ would hide the file from the user.
Put your external resources in a sub-directory of your project's WEB-INF folder. E.g., put your css resources in WEB-INF/styles and you should be able to access them as:
new File("styles/some.css");
Unless you're not using a standard WAR for deployment, in which case, you should explain your setup.
Typically resource files are placed in your war along with your class files. Thus they will be on the classpath and can be looked up via
getClass.getResource("/resources/styles/some.css")
or by opening a File as #ig0774 mentioned.
If the resource is in a directory that is not deployed in the WAR (say you need to change it without redeploying), then you can use a VM arg to define the path to your resource.
-Dresource.dir=/src/main/webapp/resources
and do a lookup via that variable to load it.
In Java web project, the standard directory like:
{WEB-ROOT} /
/WEB-INF/
/WEB-INF/lib/
/WEB-INF/classes
So, if you can get the class files path in file system dynamic,
you can get the resources file path.
you can get the path ( /WEB-INF/classes/ ) by:
this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath()
so, then the next ...