am asking this question after doing a lot of research and also implementing it in my code after the research but I ended up with FileNotFoundException.What exactly am doing here is I want to avoid hardcoding in my java code so am creating a properties file with name as Constants.properties and am calling it in my java code. but it says that it is not finding the file. My properties file is in the src folder of the project. Below is the code snippet. Any suggestions?
Properties file:
executable.run = C:\\server\\lrd.exe
incoming.file = C:\\file\\test.ic
executable.params1 = -z
executable.params2 = -a[+]
log.file = C:\\TESTFile\\test.txt
Java Code: This is the class file which has the properties file details.
public class PropInfo {
static private PropInfo _instance = null;
public String executable = null;
public String filein = null;
public String params1 = null;
public String params2 = null;
public String log = null;
protected PropInfo(){
try{
InputStream file = new FileInputStream(new File("Constants.properties"));
Properties props = new Properties();
props.load(file);
executable = props.getProperty("executable.run");
filein = props.getProperty("incomin.file");
params1 = props.getProperty("executable.params1");
params2 = props.getProperty("executable.params2");
log = props.getProperty("log.file");
}
catch(Exception e){
System.out.println("error" + e);
}
}
static public PropInfo instance(){
if(_instance == null){
_instance = new PropInfo();
}
return _instance;
}
}
Main Class:
try{
PropInfo propinfo = PropInfo.instance();
String connString = propinfo.executable + " " + propinfo.params1 + " " +
propinfo.filein + " " + propinfo.params2 + " " + " " + propinfo.log ;
Runtime rt = Runtime.getRuntime();
// Process pr = rt.exec
// (PropInfo.executable+" "+PropInfo.params1+" "+PropInfo.filein+" "
//+PropInfo.params2+" "+PropInfo.log);
Process pr = rt.exec(connString);
BufferedReader input = new BufferedReader(new InputStreamReader (pr.getInputStream()));
String line=null;
StringBuffer start= new StringBuffer();
while((line=input.readLine()) != null) {
start.append("Started" + line + "\n");
System.out.println(line);
}
// System.out.println("browse");
}
catch (Throwable t)
{
t.printStackTrace();
}
finally
{
}
Gives this Exception:
errorjava.io.FileNotFoundException: Constants.properties (The system cannot find the
file specified)
java.io.IOException: Cannot run program "null": CreateProcess error=2, The system
cannot find the file specified
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1042)
at java.lang.Runtime.exec(Runtime.java:615)
at java.lang.Runtime.exec(Runtime.java:448)
at java.lang.Runtime.exec(Runtime.java:345)
at com.emc.clp.license.StartTest.main(StartTest.java:44)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the
file specified
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(ProcessImpl.java:288)
at java.lang.ProcessImpl.start(ProcessImpl.java:133)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1023)
... 4 more
Yes, don't put your properties file into the src folder. Put it where you start the jvm from (or provide an absolute path). Also I really suggest getting rid of forward slashes in path names.
UPDATE: Add this to find out where to put your file:
System.out.println(new File(".").getAbsolutePath());
The way you load Constants.properties it should be right under your src package package at the level where your packaging starts.
for example,
if you hava src/java/propinfopackage/PropInfo
put it inside java folder and call it as follows
InputStream propertiesInputStream = null;
Properties properties = new Properties();
propertiesInputStream = PropInfo.class.getClassLoader().getResourceAsStream("/Constants.properties");
properties.load(propertiesInputStream);
String value = properties.getProperty("executable.run");
.......
I had the same problem and it was solved like this:
Properties prop = new Properties();
try {
prop.load(getClass().getResourceAsStream("/com/my/package/Constants.properties"));//here your src folder
System.out.println(prop.getProperty("executable.run"));
} catch(IOException e) {}
Make sure your properties file is in root path of the project. Right click on project and paste the properties file. Your error will go.
Also refer #Axel Answer above. It will resolve your issue.
Related
I am executing CSharp Program with java Process by creating the .exe file but the process of that file is not responding with 0 exitCode.Errors are Empty.In Visual Studio it runs fine but with java, it creates problem.No output and no Errors, I am stuck in this Please help. I am using Java 7.
I am using Csc (inbuild compiler in .Net framework for windows) It is giving me dll reference error.Command is following
csc /nologo /r:D:/simulatorConfig/ArrayConversion.dll /out:D:\\apache-tomcat-7.0.64\\temp\\tmp749792186557790590.exe D:\\apache-tomcat-7.0.64\\temp\\tmp749792186557790590.cs
stream = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
above is Error which is empty String.
code is here Please look at it.
public File compile(File sourceFile, LANGUAGE lang) throws InterruptedException, IOException, CompilerException, ConfigurationException {
String absolutePath = sourceFile.getCanonicalPath();
// System.out.println("absolutePath : " + absolutePath);
String destFile;
if (OsUtils.isWindows()) {
destFile = absolutePath.replace(lang.getFileExtension(), EXECUTABLE_FILE_SUFFIX);
} else {
destFile = absolutePath.replace(lang.getFileExtension(), "");
}
String compileCommand = generateCommand(absolutePath, destFile, lang);
logger.error("compileCommand : " + compileCommand);
// Compiles and create exe file for execution
Process proc = Runtime.getRuntime().exec(compileCommand);
// Wait for process to complete
int returnValue = proc.waitFor();
if (returnValue != 0) {
String errorMsg = getCompilerMessage(sourceFile, proc, lang);
throw new CompilerException(errorMsg);
}
proc.destroy();
return new File(destFile);
}
private String getCompilerMessage(File sourceFile, Process proc, LANGUAGE lang) throws IOException {
StringBuilder message = new StringBuilder();
BufferedReader stream = null;
String line = null;
switch (lang) {
case C:
case CPP:
// GNU C/CPP compiler prints compiler errors in standard errors
// tream
stream = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
break;
case CSHARP:
// CSharp compiler prints compiler errors in standard output stream
stream = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
break;
}
while ((line = stream.readLine()) != null) {
logger.error(line);
line = line.substring(line.indexOf(sourceFile.getName()) + (int) sourceFile.getName().length());
if (message.toString().isEmpty()) {
message.append(lang == LANGUAGE.CSHARP ? "Line" : "").append(line);
} else {
message.append("<br/>Line").append(line);
}
// message.append(line).append(SystemUtils.LINE_SEPARATOR);
}
stream.close();
return message.toString();
}
private String generateCommand(String sourceFile, String destFile, LANGUAGE lang) throws ConfigurationException {
// System.out.println("sourceFile : " + sourceFile + " -- destFile : " +
// destFile);
Configuration config = new PropertiesConfiguration("system.properties");
String cmd = "";
switch (lang) {
case C:
case CPP:
sourceFile = sourceFile.replace("\\", "\\\\");
destFile = destFile.replace("\\", "\\\\");
cmd = "g++ " + sourceFile + " -o " + destFile + " " + config.getString("C_CPP_HEADERFILE").trim();
break;
case CSHARP:
sourceFile = sourceFile.replace("\\", "\\\\");
destFile = destFile.replace("\\", "\\\\");
logger.error("Config Path : "+config.getString("MONO_PATH"));
if (OsUtils.isWindows()) {
cmd = "csc /nologo /r:" + config.getString("CS_HEADERFILE_WIN") + " /out:" + destFile + " " + sourceFile;
} else {
cmd = "/opt/mono/bin/mcs /reference:" + config.getString("CS_HEADERFILE") + " /out:" + destFile + " "
+ sourceFile;
}
break;
}
logger.info("Command :" + cmd);
return cmd;
}
When launching a command using Runtime#exec, you MUST consume the error and std streams in every case (not only when the return code is != 0), or else the internal buffers could/will get full and the subprocess will wait indefinitely until somebody consumes them.
The common symptom of this problem is an process that never return, making the whole seems to be deadlocked.
There are several ways to correct this, one the easiest is described here: https://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html#redirect-input.
Additionally, there seems to be an issue here:
// CSharp compiler prints compiler errors in standard output stream
stream = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
break;
In the comment, you tell the compiler does output errors on stdout, but you are consuming the error stream.
There was .dll linking error in when I tried to do the same in command prompt with Demo files.By keeping .dll file and .exe file in the same directory it solved my purpose program was running Flawlessly with proper exit code (0).
So kept other .dll file in this path D:\apache-tomcat-7.0.64\temp\. and is fine.
The thumb rule says .dll and .exe should be in the same directoryeory
What is the best way to load a property file when path to the property file is not fixed and provided via command line arguments?
I am using the following code to load it but it is giving me null pointer exception when run using command line arguments:
Properties p = new Properties();
p.load(testClass.class.getResourceAsStream("path to the property file"));
If you get the (full) path via command line, you jsut need to provide a reader or input stream with that path. In the end, your code could look like this:
public static void main(String[] args) {
System.out.println("reading property file " + args[0]);
// create new initial properties
Properties properties = new Properties();
// open reader to read the properties file
try (FileReader in = new FileReader(args[0])){
// load the properties from that reader
properties.load(in);
} catch (IOException e) {
// handle the exception
e.printStackTrace();
}
// print out what you just read
Enumeration<?> propertyNames = properties.propertyNames();
while(propertyNames.hasMoreElements()) {
String name = propertyNames.nextElement().toString();
System.out.println(name + ": " + properties.getProperty(name));
}
}
I'm trying to create a program that creates a copy of itself and deletes the original project folder.
(FYI: Project has its own JRE inside it)
(FYI: This program runs on Windows)
So:
To be able to do that, other than my primary main method, there is a second main method in the class called SelfUpdater.
Inside my main thread I copy the project to a second folder.
And then run the SelfUpdater's main method inside this second project folder:
This should mean that I'm running a whole new instance, totally unrelated to the first java.exe (which already gets closed with system.exit(0) as soon as new instance starts).
But when I try to delete the first folder, I get the error "Error deleting old client.java.io.IOException: Unable to delete file". Actually It deletes some of the files, but I can't delete Application.exe and its lib folder.
Its folder is not open in windows. It is not being used by anything else. I can't delete the file manually either (Windows says it is in use). And as soon as the second java.exe is terminated, I can delete it.
I can't give a total working example, But my in my main thread I call this following method:
public static void selfUpdate() {
try {
String separator = System.getProperty("file.separator");
String classpath = System.getProperty("java.class.path");
String rootPath = System.getProperty("user.dir");
String path = null;
File parentFolder = null;
File originalClientFolder = new File(rootPath);
parentFolder = originalClientFolder.getParentFile();
File secondClientFolder = new File(parentFolder.getAbsolutePath() + separator + "runLAST");
FileUtils.copyDirectory(originalClientFolder, secondClientFolder);
path = secondClientFolder.getAbsolutePath() + separator + "jre8" + separator + "bin" + separator + "java";
ProcessBuilder processBuilder = new ProcessBuilder(path, "-cp", classpath, SelfUpdater.class.getName(), downloadURL, rootPath);
processBuilder.directory(secondClientFolder);
processBuilder.start();
System.exit(0);
} catch (Exception ex) {
}
}
And my SelfUpdater class' main is:
public static void main(String[] args) {
try {
String originalClientFolderPath = args[1];
//
File oldClientFolder = new File(originalClientFolderPath);
System.out.println("Deleting old client recursively. Folder: " + oldClientFolder.getAbsolutePath());
try {
FileUtils.deleteDirectory(oldClientFolder);
} catch (Exception ex) {
}
}
}
Appearently, I forgot to change the classpath.
I'm thinking about leaving the question, since people may need such code part.
But at the end, method that's being called is changed to this:
public static void selfUpdate() {
try {
String separator = System.getProperty("file.separator");
String classpath = System.getProperty("java.class.path");
String rootPath = System.getProperty("user.dir");
String path = null;
File parentFolder = null;
File originalClientFolder = new File(rootPath);
parentFolder = originalClientFolder.getParentFile();
File secondClientFolder = new File(parentFolder.getAbsolutePath() + separator + "runLAST");
FileUtils.copyDirectory(originalClientFolder, secondClientFolder);
// ADDED: --------------------------------------------------------
String origialClientFolderName = originalClientFolder.getName();
classpath = classpath.replace(origialClientFolderName, "runLAST");
// ---------------------------------------------------------------
path = secondClientFolder.getAbsolutePath() + separator + "jre8" + separator + "bin" + separator + "java";
ProcessBuilder processBuilder = new ProcessBuilder(path, "-cp", classpath, SelfUpdater.class.getName(), downloadURL, rootPath);
processBuilder.directory(secondClientFolder);
processBuilder.start();
System.exit(0);
} catch (Exception ex) {
}
}
Hi Ive made a Java program which read lines from a CSV as data records. The problem im getting is the program works perfectly in eclipse console, however when I try to run the problem from cmd using Java compiler I doesnt work where my code specifies the location of the CSV file its show a error there as unknown. Ive tried everything such as moving the file into another package or using some other code to read the file Im getting the same result which is in eclipse it works but in the actual cmd it not working. Bellow is the ERROR and the method which reads the file.
java.io.FileNotFoundException: src\PostCodeENW.csv (The system cannot find the path specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at Java.io.FileReader.<init>(Unknown Source)
public void loadfile() {
System.out.println(System.getProperty("user.dir"));
String csvFile = "src/PostcodeENW.csv";
BufferedReader br = null;
String line = "";
try {
br = new BufferedReader(new FileReader(csvFile));
while ((line = br.readLine()) != null) {
String[] ar = line.split("/");
PostCode = ar[0].trim().replaceAll(" ", " ");
String[] ar1 = PostCode.split("\\s");
String PostCodeP1 = ar1[0];
String PostCodeP2 = ar1[1];
int Easting = Integer.parseInt(ar[1]);
int Northing = Integer.parseInt(ar[2]);
String Ward = ar[3];
Location geo = new Location(PostCodeP1, PostCodeP2, Easting, Northing, Ward);
map.put(geo.getkey(), geo.getValue());
// System.out.println(PostCodeP1 + " " + PostCodeP2 + " " + Easting + " " + Northing + " " + Ward);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.println("Done Adding all records");
System.out.println("Database currently holds: " + map.size()); //Test how much elements are currently in the HashMap
}
Where are you running the java file from? It's looking for a folder "src" in the current directory, then a file "PostcodeENW.csv" in the folder "src".
So if the command line is in a different directory to where that folder and file are located it would give you an error.
Try to run the java file in the same directory as where the folder "src" is located
In command line change directory to the eclipse workspace using cd %HOMEPATH%/Documents/workspace
If the eclipse workspace is in your user's folder, in your documents, in a folder called workspace.
Once compiled, the src path will no longer exist. As I understand eclipse, it will only jar class files from the src folder, this means, once compiled and packaged, it's unlikely that the file PostcodeENW.csv will even exist.
Two things you need to try,
First, move the PostcodeENW.csv file into the resources directory within your project
Once build and a packaged, eclipse will include the contents of the resources directory within the jar. This means that these resources will no longer be accessible as files,but will become embedded resources
Secondly, change your code so that when you read the file, you read it as an embedded resource, for example
String csvFile = "/PostcodeENW.csv";
//...
br = new BufferedReader(new InputStreamReader(getClass().getResourceAsInputStream(csvFile)));
Nb- I don't use eclipse, so I'm going what I understand needs to be done, but these are the basic ideas ;)
I am trying to create a directory but it seems to fail every time? I have checked that it is not a permission issue too, I have full permission to write to that directory. Thanks in advance.
Here is the code:
private void writeTextFile(String v){
try{
String yearString = convertInteger(yearInt);
String monthString = convertInteger(month);
String fileName = refernce.getText();
File fileDir = new File("C:\\Program Files\\Sure Important\\Report Cards\\" + yearString + "\\" + monthString);
File filePath = new File(fileDir + "\\"+ fileName + ".txt");
writeDir(fileDir);
// Create file
FileWriter fstream = new FileWriter(filePath);
try (BufferedWriter out = new BufferedWriter(fstream)) {
out.write(v);
}
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
private void writeDir(File f){
try{
if(f.mkdir()) {
System.out.println("Directory Created");
} else {
System.out.println("Directory is not created");
}
} catch(Exception e){
e.printStackTrace();
}
}
public static String convertInteger(int i) {
return Integer.toString(i);
}
Calendar cal = new GregorianCalendar();
public int month = cal.get(Calendar.MONTH);
public int yearInt = cal.get(Calendar.YEAR);
Here is the output:
Directory is not created
Error: C:\Program Files\Sure Important\Report Cards\2012\7\4532.txt (The system cannot find the path specified)
It's possibly because File.mkdir creates the directory only if the parent directory exists.
Try using File.mkdirs which creates all the necessary directories.
Here's the code which worked for me.
private void writeDir(File f){
try{
if(f.mkdirs()) {
System.out.println("Directory Created");
} else {
System.out.println("Directory is not created");
}
} catch(Exception e){
// Demo purposes only. Do NOT do this in real code. EVER.
// It squashes exceptions ...
e.printStackTrace();
}
}
The only change I made was to change f.mkdir() to f.mkdirs() and it worked
I think that #La bla bla has nailed it, but just for completeness, here are all of the things that I can think of that could cause a call to File.mkdir() to fail:
A syntax error in the pathname; e.g. an illegal character in a file name component
The directory to contain the final directory component does not exist.
There is already something with that name.
You don't have permission to create a directory in the parent directory
You don't have permission to do a lookup in some directory on the path
The directory to be created is on a read-only file system.
The file system gave a hardware error or network related error.
(Obviously, some of these possibilities can be quickly eliminated in the context of this question ...)