geting error in specifying class path when running program from cmd - java

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.

Related

Get project directory from java classes

I have a problem with System.getProperty("user.dir") giving different directory when run by IDE and when I manually compile & run it in cmd. My thing is this, I have project structure like this:
project
- exports
- src
- main
- java
- Main
- file1
- file2
One of the args in main method is the name of one of those 2 files, that I then access.
When I configure my run in IDE it works like a charm - the directory I get is C:\Users\**\**\**\project and it is able to read and write to the file.
But when I compile it in cmd javac Main.java and then run it, I get C:\Users\**\**\**\project\src\main\java and because of that, I am unable to access the file without having to modify the path.
My question is, is there like a golden way, that would work for both these cases, without me having to alter the returned path?
EDIT:
For clear understanding, I know what System.getProperty("user.dir") returns, but my question was, if it is possible to get the same result somehow with using Path or if I have to get the path and edit it, so that it will end in project directory?
in IDE I get: C:\Users\petri\Desktop\CZM\bicycle-statistics
in cmd: C:\Users\petri\Desktop\CZM\bicycle-statistics\src\main\java
I want to get the same path in cmd, that I got in IDE.
I tried using Paths.get("").toAbsolutePath(), but it is the same thing.
So, what I did is this:
Path path = Paths.get("").toAbsolutePath();
while (!path.endsWith("project")) {
path = path.getParent();
}
And it works, but I am trying to ask, if there is some more elegant way, because I will have to defend my solution in front of my supervisor.
Normally your IDE will build source files in src/main/java and write the class files out to some other directory, like target/classes.
If your IDE built the project that way, then you can run it from the command line by switching to your project directory (cd C:\Users\**\**\**\project using your example) and then running:
java -classpath target/classes Main
assuming that target/classes is where your IDE put the files. If you really do have the class files in the source directory, then use -classpath src/main/java.
If you always run the program from the project directory, then you can assume within the program that the current directory is the project directory. You don't even have to use user.dir then, just use relative path names for everything, e.g., path/to/whatever.dat will automatically resolve to C:\Users\**\**\**\project\path\to\whatever.dat.
One of the args in main method is the name of one of those 2 files
Then make sure you enter the name correctly.
E.g. if the current working directory is the project folder, then name file1 will refer to the file1 file. If the current working directory is the java folder, then the argument to the program needs to be ..\..\..\file1.
That is because you give relative file names, which means they are relative to the current working directory.
Alternatively, give a fully qualified name, then the argument will be the same, regardless of what the current working directory is:
C:\Users\**\**\**\project\file1

Resource path change during jar execution

I'm trying to read a json file in a Resources directory and I use the following:
jsonObject = this.readJson(this.getClass().getClassLoader().getResource("jsonFileName").getPath());
In the IDE it runs correctly but when I build thw jar and try to run it by java -jar jarName I get a "File not found" Error and when I checked the path, it looks like this:
...projectName/target/projectName-1.0-SNAPSHOT.jar!/kb/is/identity.json
When running on the IDE the paths looks like this:
...projectName/target/classes/kb/is/identity.json
It's important to use getResourceAsStream, not getResource.
Have a look at How getClassLoader().getResourceAsStream() works in java
getResource("jsonFileName") - in this case root directory is project name using it under Idea, but when you run it under jar - I think that root path is User Home.
If I remember correctly, you can fix it with using / in resource path.
E.g. when using Maven, you have a resource directory. You have identity.json in the resource root. Using getClass().getResourceAsStream("/identity.json") receive this file (in Idea and in jar), because when you build a jar, all resources are copied to the root of the jar file.

Accessing executing folder when running a jar file

I would like to be able to run a jar file java -jar myapp.jar on different folders and have it load config.properties based on the executing context.
/myapp/myapp.jar
/folder1/config.properties
/folder2/java -jar /myapp/myapp.jar <------ loads /folder1/config.properties
/some/folder2/config.properties
/some/folder2/java -jar /myapp/myapp.jar <------ loads /folder2/config.properties
Once the properties are loaded, I want it to then create some files in the current execution folder.
So:
How do I tell java to load a properties file based on the current executing context?
How do I get access the folder that the jar was executing from?
When you provide a relative path to the constructor of the class File like new File("config.properties"), behind the scene, the absolute path built is
System.getProperty("user.dir") / config.properties
with user.dir that is actually the User working directory which is also the directory from which you launch your command.

Java: File path error

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());

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.

Categories

Resources