Java: File path error - java

File defaultCss=new File(this.getClass().getResource("application.css").getFile());
PiChart.getScene().getStylesheets().add("file:///" + defaultCss.getAbsolutePath().replace("\\", "/"));
The above line in Controller.java fetches the required resource in Eclipse while running, but when exported to an executable JAR, it is not fetching the file.
Because:
In Eclipse the line fetches
src/com/piscope/application.css
In JAR, the path is:
com/piscope/application.css
Please let me know the path to be set so that one can run both eclipse and JAR executions without errors.
Note: Since the file is the source for the software package,the file needs to be inside the JAR file.

Just use the following (no need to format the syntax by yourself):
PiChart.getScene().getStylesheets().add(this.getClass().getResource("application.css").toExternalForm());

Related

How do I use a classloader to get inputstream on a model.zip file where the zip file is wrapped in a .jar file in classpath

The issue:
We have a jetty web-app, and in the application-code, I am trying to access a zip within a jar in classpath. Here's the jar in the libs folder:
/path/to/app/x.x.0-SNAPSHOT/apps/libs/my-model.jar
where my-model.jar is really just an empty folder with model.zip file inside it. If I extract this jar, I get johnsnow/mymodel.zip
My application code tries to access this zip as:
getClass().getResourceAsStream("johnsnow/mymodel.zip")
but of course, I don't get a proper handle to this resource and wind up getting a nullpointer exception. What am I doing wrong? Shouldn't I be able to access a file within a jar file in classpath using the getClass().getResourceAsStream() method?
Footnote:
Because model.zip was too large, we decided against shipping it with the code base. Thus we pushed it into a nexus repository, and reference the jar via a gradle compile dependency as follows:
compile "com.company.group.nlp:my-model:1.0#jar"
The fact that building the distribution pulls this jar, and puts it in apps/libs tells me that gradle does its part (of downloading the dependency to a classpath). The issue remains that I can't seem to find a way to access mymodel.zip inside my-model.jar
Try adding a slash to the file path:
getClass().getResourceAsStream("/johnsnow/mymodel.zip");
It will tell java to start looking for the class from the root folder, not from the current class package.

how to define a file path in java to make it ready for production phase?

I am writing a program in java with netbeans IDE which receives a jasper report *.jrxml and then displays the report for the user. I wrote the following line of code for the file path
String reportSource = "src\\jasper-reports\\report.jrxml";
but when I move the dist folder in some other place and try to run the jar file inside it, my program can not find the report.
my problem is that were should I put the *.jrxml file and how to define it's path in my program so that when I want to give my software to someone else it runs without any errors (e.g. the program can find the file)
avoid using absolute paths. try to include the file as a resource in your netbeans project. then in your code you can search for and load the file as
new InputStreamReader((Main.class.getResourceAsStream("/report.jrxml")))
something like that depending on where the file resides in your project
it's more recommended using one of the two approaches:
pass the locations/paths as a -Dproperty=value in the Java application launcher command line http://www.tutorialspoint.com/unix_commands/java.htm
store it the locations/paths in a configurations file with a unique key and edit the file accordingly for different environments,
e.g.this files always stored in ${HOME}/config_files/ directory
absolute paths considered a bad practice

Error: Could not find or load main class net.sourceforge.cobertura.merge.Main

I am getting this strange error while excuting following commands..
cobertura-merge.bat --auxClasspath ./cobertura-2.0.3.jar --datafile cobertura.ser cobertura1.ser cobertura2.ser
cobertura-merge.bat --auxClasspath . --datafile cobertura.ser cobertura1.ser cobertura2.ser
Error -
Error: Could not find or load main class net.sourceforge.cobertura.merge.Main
PS -
I have JAVE HOME set.
Java bin dir is added to PATH.
CLASSPATH is defined with - %CLASSPATH%;.;.
I have added cobertura-2.0.3.jar in jdk lib and jre lib directory.
I ran in to the same problem. To fix this, you will need to edit the cobertura-merge.bat in a text editor. The last line looks something like this:
java -cp "%COBERTURA_HOME%cobertura.jar;%COBERTURA_HOME%lib\asm-3.3.1.jar;.....
The jar file names in this command most likely do not match the jar file names in your cobertura_home\lib.
Example:
In the command above, from the batch file, it references 'cobertura.jar'
If you look in the lib folder, the actual name of the file is 'cobertura-2.0.3.jar
The same goes for the other jar files. So you will have to change the bat file, or the file names, to make them match.

geting error in specifying class path when running program from cmd

I am trying to run program from command prompt
Here is my director structure
In the classes directory i have this structure
In the email folder i have two properties file general-mail-settings.properties and customer-mail-settings.properties
Now when i run the command
D:\vintnes\lasses>java -cp ".;..\dependency-jars\*" com/softech/ls360/integration/BatchImport vintners
Then i get the error that
java.lang.Exception: Email Properties File not found: src\main\resources\email\general-mail-settings.properties (The system cannot find the path specified)
at
...
I tried this to specify path
java -cp ".;..\dependency-jars\*;.\email\*.*" com/softech/ls360/integration/BatchImport customer
But still i am getting the error. I tried ;email\* and \email\*, but still i am getting the error. How can i specify path so program get run?
Thanks
You put a path to src/main/resources in your code somewhere. This is a directory used by Maven builds to hold "resource" files (files that aren't code but that should be copied into the finished artifact, like configuration files or media). The contents of src/main/resources are copied directly into the root of the artifact as-is, so in this case, the email directory is copied straight into your classes directory. Remove the src/main/resources part of the path from your properties lookup.

Java path to support both IDE and generated JAR

I have an issue with path names in my code. Let's say I have a main class:
com.test.LoadFile.java
Similarly I have a myxml.xml file under com.test. Meaning that the Java file and XML file are under same package.
Can somebody suggest how, when I do (inside LoadFile)
File file = new File("???/myxml.xml")
What should the path be, to support both:
Eclipse IDE code (after including the above code into a single Java project)
and
Run the main LoadFile class outside of the IDE (in a JAR file)
What should I use as the value of the path variable to include in the generated project JAR?
You can read the XML file using getResourceAsStream(), as long as it's in the CLASSPATH:
InputStream is = LoadFile.class.getClassLoader().getResourceAsStream("/myxml.xml");
EDIT: If you are packaging into a .jar, you must specify the complete path of the resource from the jar's root folder using "/" at the beginning of string

Categories

Resources