im4java FileNotFoundException - filenotfoundexception

When I use ImageMagick+im4java in eclipse, I always has this error:
Exception in thread "main" org.im4java.core.CommandException: java.io.FileNotFoundException: convert
at org.im4java.core.ImageCommand.run(ImageCommand.java:219)
at imagetools.ImageTools.resizeImage(ImageTools.java:71)
at imagetools.ImageTools.main(ImageTools.java:92)
Caused by: java.io.FileNotFoundException: convert
at org.im4java.process.ProcessStarter.searchForCmd(ProcessStarter.java:661)
at org.im4java.process.ProcessStarter.startProcess(ProcessStarter.java:403)
at org.im4java.process.ProcessStarter.run(ProcessStarter.java:312)
at org.im4java.core.ImageCommand.run(ImageCommand.java:215)
And I am sure that the itself ImageMagick works well on my computer(Mac OSX 10.10.1).
Here is my code:
package imagetools;
import org.im4java.process.ProcessStarter;
import org.im4java.core.ConvertCmd;
import org.im4java.core.IMOperation;
public class ImageTools {
public static void resizeImage(int width, int height, String srcPath, String newPath) throws Exception {
IMOperation op = new IMOperation();
op.addImage(srcPath);
op.resize(width, height);
op.addImage(newPath);
ConvertCmd convert = new ConvertCmd();
convert.run(op);
}
public static void main(String[] args) throws Exception{
String src = "/Users/lvxinyun/Downloads/im4java-1.4.0/images.src/rose1.jpg";
String n = "/Users/lvxinyun/Downloads/im4java-1.4.0/images.src/rose1_n.jpg";
ProcessStarter.setGlobalSearchPath("/Users/lvxinyun/Downloads/ImageMagick-6.9.0-5");
resizeImage(600,800,src,n);
}
}

cd C:\Program Files\ImageMagick-7.0.5-Q16
copy magick.exe convert.exe

You have to install ImageMagick-6.9.1-10-Q16-x64-dll.exe and set IM4JAVA_TOOLPATH = "C:\Program Files\ImageMagick-6.9.1-Q16 (your install path)
You can look here for more details: http://im4java.sourceforge.net/docs/dev-guide.html

It is important to set the class path.
Download latest version of ImageMagick and exiftool.
Install ImageMagick.
Create a String var using the installation directory.
String myPath = "C:\\Program Files\\ImageMagick-7.0.8-Q16;C:\\Program Files\\exiftool-11.53";
Set it as OS command as mentioned below:
ProcessStarter.setGlobalSearchPath(myPath);
Reference

Related

why I cannot read a file from Spring-boot's app resources directory?

Seems like whatever I do I cannot read a file from resources dir in my Spring-boot app.
Any ideas?
Could it be because of the way I call the method to read the file?
#SpringBootApplication
public class DemostoreApplication {
public static void main(String[] args) throws IOException {
SpringApplication.run(DemostoreApplication.class, args);
}
#EventListener(classes = ApplicationStartedEvent.class)
public void applicationStarted() {
loadAFile();
}
void loadAFile(){
InputStream inputStream = getClass().getResourceAsStream("user-data.txt");
}
}
I can see that there are many questions like this one, mostly unanswered. I've tried the propositions in all of them.
Yes, I go the answer.
A / should be added, denoting relative to the root of the project!
Otherwise the file is looked for in the directory relative to the class's package. Stupid me.
This is working:
InputStream is = getClass().getResourceAsStream("/user-data.txt");

TeamCity and download folders

We are running Java-Cucumber-Maven automated test scripts from TeamCity. Up to now this has worked very well but our current test requires that several .CSV files are downloaded for analysis by the script. There's a
ReadCsvFiles.java
file in our code and that dictates where the downloaded files are located....easy for running tests locally but we're having real problems with the path to the build agent in TeamCity..
public class ReadCsvFiles {
//public static String sFileLocation = "C:\\Users\\ankit.bisht\\Downloads\\";
public static String sFileLocation = "C:\\BuildAgent\\temp\\buildTmp\\";
//public static String sFileLocation = "//users/redmayned//Downloads/";
public static void depositsHeldReports() throws Throwable {
String csvFile = sFileLocation + "DepositsHeld.csv";
CSVReader reader = null;
try {
reader = new CSVReader(new FileReader(csvFile));
String[] column;
boolean doesExist = false;
while ((column = reader.readNext()) != null) {
The commented out entries are for local windows and local OSX. The middile one is just one of many options we've tried...all resulting in an error message;
java.io.FileNotFoundException: C:\BuildAgent\temp\buildTmp\DepositsHeld.csv (The system cannot find the file specified)
Can anyone save my sanity please?
Thanks
David

jar file not up updating values in file

I have a javafx project that I am preparing for distributions. The project works perfectly from the debugger inside NetBeans. I am working on Linux.
But when I run the project from the jar file, there is one function in all the others that does not work. This function is supposed to open the settings file on a button click and alter some values from true to false.
I have changed the settings file's location and tried around google, but all to no avail.
I am still quite a newbie to java, fx, netbeans and java (not so much programming) and making my first experiences.
Any idea why this happens?
#FXML
private void openSettingsFile(ActionEvent event) throws IOException {
// this test works ....
ProcessBuilder processBuilder = new ProcessBuilder("terminal");
processBuilder.start();
// this part only replaces the values when I use the debugger ..
Path path = Paths.get("src/desktop_launcher/Settings.java");
Charset charset = StandardCharsets.UTF_8;
String content = new String(Files.readAllBytes(path));
content = content.replaceAll(" \"true\"" , " \"false\"");
Files.write(path, content.getBytes(charset));
Your approach (which, as far as I understand it, is to try to programmatically change the source file that generates the properties file) will not work at deployment time for a number of reasons.
The first is that the source files are generally not available at runtime: your jar file contains the class files and other resources required to run the application, but typically not the source code (and it's not desirable to include it in the application, in general).
Secondly, you are trying to locate this file from a relative path passed to Paths.get(..). This will resolve relative to the working directory, which is essentially arbitrary (basically "where the application was run from"). So even if the source code were available at runtime, this would not be a reliable way to find it. (My guess is that your debugger runs with the working directory fortuitously set to the parent directory of src, but when you run the jar file the most likely location of the working directory is the directory in which the jar file is located. But that is just a guess: it really depends on the configuration of your IDE, debugger, etc etc.)
Thirdly, and probably most importantly, even if the code does find the source file and rewrite it, that's all it will do. Next time you execute the application from the jar file, it won't magically know there is a new version of the source code that has to be compiled and then the resulting class file(s) incorporated into the jar file. So you would have to also include code to compile the new version of your source code (where will you get a compiler? AFAIK not all Java runtimes will include a compiler) and then programmatically insert the new class file(s) into the jar file (how do you even figure out where the jar file is: that is certainly non-trivial and I don't think it can be done in a reliable manner). What if the current user doesn't have permissions to write the directory containing the jar (which is a pretty common scenario..)?
The usual way to load and save startup configuration values is to use the java.util.Properties API. You need an external location to store the properties file, that you can be certain exists on the user's computer: a convenient way to do this is to create an application-specific directory in the user's home directory. The user's home directory can be accessed via System.getProperty("user.home"); (The system property user.home is one of those that is guaranteed to exist.).
I would recommend using a separate class to manage the configuration properties. For example:
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.util.Properties;
public class PropertiesAccessor {
private static final Path USER_HOME = Paths.get(System.getProperty("user.home"));
private Properties props ;
private Path path ;
public PropertiesAccessor(String relativePath) {
path = USER_HOME.resolve(relativePath);
props = new Properties();
if (Files.exists(path)) {
try {
props.load(Files.newBufferedReader(path));
} catch (IOException exc) {
System.err.println("Warning: could not load properties file. Using defaults.");
exc.printStackTrace(System.err);
loadDefaults();
}
} else {
loadDefaults();
}
}
public Boolean getBooleanValue(String key) {
String value = props.getProperty(key);
return value == null ? null : Boolean.valueOf(value) ;
}
public void updateBooleanValue(String key, boolean value) {
props.setProperty(key, Boolean.toString(value));
}
public void writeProperties() throws IOException {
if (! Files.exists(path)) {
Files.createDirectories(path.getParent());
Files.createFile(path);
}
props.store(Files.newBufferedWriter(path), "Properties updated "+LocalDateTime.now());
}
private final void loadDefaults() {
// in real life, you might keep a default properties file bundled with
// the application and read that here, e.g.
// props.load(getClass().getResourceAsStream("/default-startup.properties"));
props.setProperty("config.value1", "true");
props.setProperty("config.value2", "false");
}
}
And now you can use this in your application. Just load the properties in the init() method and save them back in the stop() method. Note that executing this will create a directory called .myApp in your home directory, and a file called startup.properties inside it.
import java.io.IOException;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class StartupPropertiesExample extends Application {
private PropertiesAccessor config ;
private CheckBox value1 ;
private CheckBox value2 ;
#Override
public void init() {
config = new PropertiesAccessor(".myApp/startup.properties");
}
#Override
public void start(Stage primaryStage) {
value1 = new CheckBox("Value 1");
value2 = new CheckBox("Value 2");
value1.setSelected(config.getBooleanValue("config.value1"));
value2.setSelected(config.getBooleanValue("config.value2"));
Button exit = new Button("Exit");
exit.setOnAction(e -> Platform.exit());
VBox root = new VBox(10, value1, value2, exit);
root.setPadding(new Insets(10));
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
#Override
public void stop() {
config.updateBooleanValue("config.value1", value1.isSelected());
config.updateBooleanValue("config.value2", value2.isSelected());
try {
config.writeProperties();
} catch (IOException exc) {
System.err.println("Warning: could not save properties");
exc.printStackTrace(System.err);
}
}
public static void main(String[] args) {
launch(args);
}
}

FileCopyUtils Springframework

I want to copy a file using spring FileCopyUtils.
this is the first time I used
I followed a tutorial and I get this exception
package com.sctrcd.multidsdemo.integration.repositories.foo;
import java.io.File;
import java.io.IOException;
import org.springframework.util.FileCopyUtils;
public class CopyTest {
public static void main(String[] args) throws InterruptedException,
IOException {
File source = new File("‪C:\\Users\\Momo Kh\\Desktop\\CV.pdf");
File dest = new File("C:\\Users\\Momo Kh\\Desktop\\Test\\CV.pdf");
FileCopyUtils.copy(source, dest);
}
}
And i have this Exception
Exception in thread "main" java.io.FileNotFoundException: ‪C:\Users\Momo Kh\Desktop\CV.pdf (La syntaxe du nom de fichier, de répertoire ou de volume est incorrecte)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:146)
at org.springframework.util.FileCopyUtils.copy(FileCopyUtils.java:63)
at com.sctrcd.multidsdemo.integration.repositories.foo.CopyTest.main(CopyTest.java:15)
Either you don't have the file or you don't have the necessary priviliges to touch it. Try some directory like C:\\Momo Kh\\CV.pdf instead. Maybe you can't access stuff under user.
This code works (The same as the last with some change)
I think it was a bug
package com.sctrcd.multidsdemo.integration.repositories.foo;
import java.io.File;
import java.io.IOException;
import org.springframework.util.FileCopyUtils;
public class CopyTest {
public static void main(String[] args) throws InterruptedException,
IOException {
File source = new File("C:\\Users\\Momo Kh\\Desktop\\CV.pdf");
File dest = new File("C:\\Users\\Momo Kh\\Desktop\\files\\destfile1.pdf");
long start = System.nanoTime();
long end;
// copy file using Spring FileCopyUtils
start = System.nanoTime();
FileCopyUtils.copy(source, dest);
end = System.nanoTime();
System.out.println("Time taken by Spring FileCopyUtils Copy = " + (end - start));
}
}
And the result
Time taken by Spring FileCopyUtils Copy = 41100377

POI insert photo into excel but fail

below is my code copied from somewhere on the web used to copy an image file to a excel file worksheet.
but it has error on running statement
"int my_picture_id = my_workbook.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG);"
with error "Exception in thread "main" java.lang.NoClassDefFoundError:
org/apache/commons/codec/digest/DigestUtils
at org.apache.poi.hssf.usermodel.HSSFWorkbook.addPicture(HSSFWorkbook.java:1610)
at Addphoto.main(Addphoto.java:19)"
I cannot find the reason as i am really a newbie on java. Please help to solve this trouble.
import java.io.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.*;
import org.apache.poi.util.IOUtils;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
public class Addphoto {
public static void main(String[] args) throws Exception{
HSSFWorkbook my_workbook = new HSSFWorkbook();
HSSFSheet my_sheet = my_workbook.createSheet("MyBanner");
InputStream my_banner_image = new FileInputStream("C:/path/123.jpg");
byte[] bytes = IOUtils.toByteArray(my_banner_image);
int my_picture_id = my_workbook.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG);
my_banner_image.close();
HSSFPatriarch drawing = my_sheet.createDrawingPatriarch();
ClientAnchor my_anchor = new HSSFClientAnchor();
my_anchor.setCol1(2);
my_anchor.setRow1(1);
HSSFPicture my_picture = drawing.createPicture(my_anchor, my_picture_id);
my_picture.resize();
FileOutputStream out = new FileOutputStream(new File("C:/path/uploadphoto-test.xls"));
my_workbook.write(out);
out.close();
}
}
I hope you get your answer.....
But to help other I am posting this.
This problem happens if you are missing the JAR file commons-codec-1.7.jar which is a part of Apache Commons Codec distribution. POI needs this JAR file to add the image to the workbook. So, make sure you have this JAR file (or equivalent version) in your classpath.

Resources