How can I resolve Windows vs Linux properties file paths? - java

I have a java project on a Windows box which builds successfully and all tests pass locally. This project is then checked in and built with Jenkins on a Linux box.
The problem I am having is related to path issues in my properties file which is used for running tests. I point to a resources dir for part of a file path like this: "./src/test/resources".
I am trying to access two files which reside in the same directory. File1 is accessed successfully. Then File2 access is attempted, but it returns a file not found exception.
I've tried using an absolute path like this: "/code/myproject/main/src/test/resources/..." This again works on my local box, but not on Jenkins because Jenkins only knows about" "/myproject/main/src/test/resources/..."
How can this be resolved?

Today I learned Linux is case sensitive. The file name extension is upper case, but I was pointing to a lower case one.

Depending on your version of Windows... Windows 7 (don't know since when this was implemented) understand linux-style / separator just fine, as long as the whole path is relative.

Related

How to use Gecko driver in Dynamic Web Application

I am using Selenium to crawl a website using Java and Gecko driver. When I run the project on a local Tomcat v.9 server I am able to get the path of the Gecko driver directly. But what I want is to be able to run the Gecko exe from within the project folder. The image below shows the project hierarchy. I have added the exe in the "resources" folder under "src". How do I get the path of the driver to use it in my code?
Project hierarchy
Turns out it was pretty easy. I noticed that Catalina base has a similar directory to where WEB-INF is. So I did the following;
String catalinaBase= System.getProperty("catalina.base");
so the output is;
C:\Users\<user>\EE
Workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1
Which means I would have to keep this Path up until the ".metadata" part (excluding it).
catalinaBase = catalinaBase.split(".metadata")[0];
I then excluded anything after the "EE Workspace" by splitting the Path String.
mainPath = catalinaBase + "\\Crawler\\src\\main\\webapp\\WEB-INF\\geckodriver.exe";
I concatenated the Path String I got from step no.2 and the rest of the path I know is the same.
System.setProperty("webdriver.gecko.driver",mainPath);
Last but not least I set the system property for gecko driver using the final Path String I created on step 3.
Bear in mind. I am unaware if this is working when you extract the WAR file. For a local Tomcat server it will work.
If someone can let me know if this works when a WAR is exported and
deployed on a real Tomcat server I would appreciate it.

Jenkins & Java : How to specify input file location

I have a java project which compares data in two excel files and displays the output. IN my eclipse project i created a folder data and in the code I wrote code read from root/data and it works fine as well. But my manager asked me to move this job to Jenkins. So my question is how do i specify the input folder path in Jenkins , Should it be the same server where Jenkins is installed or Jenkins can read data from another location in another server ?
By default, Jenkins will work on the Job's workspace location, if you provide a path in the job (be it via Parameter or Env. variable etc), it will be relative to that location.
However, you can specify an absolute path for anywhere on the Jenkins Server, which will also work.
If you wish to read data from another server, you will need to make it available to the job's runtime/access level.
One example would be to put this file on IIS or Network Share or other form of sharing, and download it during your chef job into the workspace.
Powershell example for downloading a file from IIS site:
$source = "http://my-web-server-ip/download/mycsvfile.csv"
$destination = "c:\my-jenkins-job-workspace\mycsvfile.csv"
Invoke-WebRequest $source -OutFile $destination
Please consider the above is just a basic implementation of this, and this can be accomplish in a number of ways - which some may be better than others.

Can't locate a file inside the project

Okay; So I've been using Windows for quite some time, however I've recently moved over to a linux-based operating system. I've ran into numerous issues such as the File-System for Linux being case-sensative. IE: Filename.PNG will not load if you try to load Filename.png.
Anyway, That's not my problem here, and I'm kind-of confused.
Here's my problem, in cropped picture format, code and all.
I've tried varations, such as "/SpriteSheets/tileset.jpg", "assets/SpriteSheets/tileset.jpg"
However, I can't get it to work.
Well it seems your file name is tileset.png and not tileset.jpg.
Print your current path first with a System.out.println(new File(".").getAbsolutePath()) and then try and see where you should point your path in the program.
EDIT: Eclipse usually points your path inside the workspace, I've had trouble with that previously. You can set the runtime path to a different directory; Go to Run configurations, choose your configuration, choose tab Arguments and set the working directory (at the bottom) to a directory of your choosing.

Java App unexpected behavior when exported

I have a very strange problem, that I can't figure out, the thing is that my aplication runs perfectly on the IDE (Eclipse), but not when exported, when I run the jar (double click) the aplication start but some functionality is missing (loading from a template file, but this does not happend when loading from a normal file), when I try to run it from console (java - jar my.jar) in order to see any error message it turns out that my aplication works perfectly fine! :S ...
Some more info:
My app is running over windows 7
I start the task manager, and I noticed that when I start my aplication using double click its under the name java.exe *32, and when I do it from command line its under the name java.exe (without "*32"), as far as I know I programmed nothing related to a 32 or 64 bits functionallity.
"Solved"
Well I was not able to solve it the way I wanted, as far as I was able to find, i found that there were a problem between the 2 java versions I was running x32 & x64, I deleted the 32 bit version and it start working as a charm, but I'm still not sure about what happend, I give my thanks to #Sajal Dutta one of its comments help me to understand part of the problem, thanks to all of you anyway, I'll keep searching until I find the problem...
When you create a jar from Eclipse, your assets don't get copied over to jar or location is not preserved. Open the jar and check if you have your templates in the right location or you have it at all.
To have the exported jar include your assets/resources-
Right click on your project in Eclipse. Then New -> Source Folder.
Name the source folder anything. e.g. template_src.
Copy or drag the entire directory of your template to template_src. Then make the jar.
Since it works via the command line but not when double-clicking the jar, it is likely that the working directory is different (and that you're loading the template with a relative path). When you run an executable jar by double-clicking, on some operating systems, the working directory is the home directory whereas when you run from the command line, it's the directory you're currently in.
The "files" in the jar are not handled by File, but are resources;
URL url = getClass().getResource("...");
InputStream in = getClass().getResourceAsStream("...");
Then, the file paths inside a jar, or on a non-Windows platform are case-sensitive.
"Template_A.xml"
is not
"template_a.xml"
Also you might inspect the jar with 7zip or WinZip.

Permissions for Java application on Ubuntu

I have a NetBeans RCP application that's currently working on Windows and I'm trying to make Linux compatible. The application creates folders and files and modify files as well.
It works fine on Windows without any modification but on Ubuntu it fails creating folders during start up. I know it's a permission issue.
What are my options?
Can the application itself assign the permissions it needs like by running a script using ProcessBuilder?
Thanks in advance!
It all depends on who you are when running the process on Ubuntu, and the path of the folders that you're trying to create. Does this user have permissions to create the folders in that directory? What sort of data are you writing out to disk? Can you use a platform neutral mechanism thats user oriented, like Java Preferences or perhaps:
System.getProperty("user.home")
-or-
System.getProperty("java.io.tmpdir")?
You either need to create required folders as part of a setup process or restrict your IO to folders you have access to (the users home and the temp folder). Notice that on Linux there are standard locations where many folders should be placed and that administrators will frown upon applications that do not follow these standards.
Can you tell what files/folders you need for what purpose?
Looks like the cause of the problem is the difference in path delimiter between Windows and Linux. On linux you should use normal slashes. The error mentions the path:
/home/javier\marauroa.trace.db
As the \ is not a path delimiter but the escape character it is trying to create a file in the folder /home where it does not have permissions.
The path should be:
/home/javier/marauroa.trace.db
You might want to consider putting your apps files in a subfolder called .yourappname so then it would become
/home/javier/.yourappname/marauroa.trace.db
This is what many unix applications do and hide it in normal file listings. To get the path seperator for the system your application is running on you can use the following static field:
java.io.File.seperator

Categories

Resources