im4java throws FileNotFoundException error - java

I need to use ImageMagic in my java project.
I already installed ImageMagic-7.0.3 on Windows 10
(command line works fine)
Then, i added dependency to im4java in maven file. When i try to do some action like:
public static void main(String[] args) {
String imPath="C:\\Program Files\\ImageMagick-7.0.3-Q16";
ConvertCmd cmd = new ConvertCmd();
cmd.setSearchPath(imPath);
IMOperation op = new IMOperation();
op.addImage("biuro.jpg");
op.negate();
op.addImage("biuro_new.jpg");
try {
cmd.run(op);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IM4JavaException e) {
e.printStackTrace();
}
System.out.println("works");
}
i get following error
org.im4java.core.CommandException: java.io.FileNotFoundException: convert
at org.im4java.core.ImageCommand.run(ImageCommand.java:219)
at Main.main(Main.java:30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: java.io.FileNotFoundException: convert
at org.im4java.process.ProcessStarter.searchForCmd(ProcessStarter.java:661)
at org.im4java.process.ProcessStarter.startProcess(ProcessStarter.java:399)
at org.im4java.process.ProcessStarter.run(ProcessStarter.java:312)
at org.im4java.core.ImageCommand.run(ImageCommand.java:215)
... 6 more

I went trough half of Internet. Read half of google. There was only one small advice saying "have you installed Visual C++ Redistributable and reboot?" That was the solution.
ImageMagick worked in command line, so I did not expected requirement of that type. Moreover I did not find a thing about it in both - im4java and ImageMagick reference.
Hope this will be helpful, and noone ever will waste about 5h of life searching the answer.

Set the user variable under environment variable,Variable name as IM4JAVA_TOOLPATH
and Variable Value as C:\Program Files\ImageMagick-7.0.3-Q16.
after that kill all the java process and try to run the program.
Other way you can set the global search path like below:
String myPath="C:\\Programs\\ImageMagick;";
ProcessStarter.setGlobalSearchPath(myPath);
This will override your user environmental variable.
Also refer the documentation for more setup related info in detail http://im4java.sourceforge.net/docs/dev-guide.html

After spending a lot a hours in order to research why I had that issue, the solution is : you need to uninstall then reinstall ImageMagick with "Install legacy utilities (e.g. convert)" during the setup.

Related

Externally executable application fails for some application?

I'm wanting to launch the program from a Java application, with some luck. Most programs are started without problems, but some seem to not execute properly(?).
The code I'm using is very simple:
private static void exec() {
ProcessBuilder builder = new ProcessBuilder("C:\\Users\\Fillipuster\\AppData\\Local\\Discord\\Update.exe");
try {
builder.start();
} catch (IOException e) {
e.printStackTrace();
}
}
...and works for almost all executables (*.exe). Discord is purposefully placed in the example, as it is one of the programs that cause this problem. (along with Messenger For Windows and GOG Galaxy).
The behavior is simple, and the same for all executable that causes this; a command prompt quickly pops into existence and then promptly disappears (pun intended) - resulting in the application not being launched.
Sifting through Google and Stack Overflow proved a futile effort, and at this point, I'm at a complete loss.
Any help/input is much appreciated.
Thanks to John, who pointed out that even launching the the Update.exe file "manually" results in the same behavior, I've found the problem.
It seems that when launching Discord successfully, one is actually launching a shortcut that gives a parameter to the executable. In this case:
--processStart Discord.exe
This means that the following code will in fact start Discord:
private static void exec() {
ProcessBuilder builder = new ProcessBuilder("C:\\Users\\Fillipuster\\AppData\\Local\\Discord\\Update.exe", "--processStart", "Discord.exe");
try {
builder.start();
} catch (IOException e) {
e.printStackTrace();
}
}
Thanks to John and all the other commenters.

Manually loading native libraries to circumvent a restrictive environment

I'm maintaining a Java Swing application that requires a connection to an instance of Microsoft SQL Server. For various reasons, I opted to replace the native SQL Server driver being used with jTDS (the aforementioned Microsoft drivers were not working at the time and have apparently failed in the field as well). When I try to run the executable .jar outside of the IDE, I run into issues because I'm missing the appropriate ntlmauth.dll dependency.
Before proceeding, it's important to note that this application is being developed and used in an extremely restrictive (Windows-only) environment:
I cannot install any software that requires Windows UAC authentication
My users cannot install or run any software that requires UAC authentication
This currently means I cannot write files to System32 or JAVA_HOME, and cannot use any sort of ProcessBuilder tomfoolery to start another JVM with whatever command line arguments I need
I cannot use executable wrappers/installers that would only require the UAC permission for the first time installation/setup
The solution I'm trying is a combination of this one and this one to check it--essentially packaging the .dll inside of the .jar, then extracting it and loading it if necessary--as most of the other solutions I've found have been incompatible with the above restrictions; however, I'm running into an issue where even after the native library is ostensibly "loaded," I get an exception saying it isn't.
My pre-startup code:
private static final String LIB_BIN = "/lib-bin/";
private static final String JTDS_AUTH = "ntlmauth";
// load required JTDS binaries
static {
logger.info("Attempting to load library {}.dll", JTDS_AUTH);
try {
System.loadLibrary(JTDS_AUTH);
} catch (UnsatisfiedLinkError e) {
loadFromJar();
}
try {
// do some quick checks to make sure that went ok
NativeLibraries nl = new NativeLibraries();
logger.debug("Loaded libraries: {}", nl.getLoadedLibraries().toString());
} catch (NoSuchFieldException ex) {
logger.info("Native library checker load failed", ex);
}
}
/**
* When packaged into JAR extracts DLLs, places these into
*/
private static void loadFromJar() {
// we need to put DLL in temp dir
String path = ***;
loadLib(path, JTDS_AUTH);
}
/**
* Puts library to temp dir and loads to memory
*/
private static void loadLib(String path, String name) {
name = name + ".dll";
try {
// have to use a stream
InputStream in = net.sourceforge.jtds.jdbc.JtdsConnection.class.getResourceAsStream(LIB_BIN + name);
// always write to different location
File fileOut = new File(System.getProperty("java.io.tmpdir") + "/" + path + LIB_BIN + name);
logger.info("Writing dll to: " + fileOut.getAbsolutePath());
OutputStream out = FileUtils.openOutputStream(fileOut);
IOUtils.copy(in, out);
in.close();
out.close();
System.load(fileOut.toString());
} catch (Exception e) {
logger.error("Exception with native library loader", e);
JOptionPane.showMessageDialog(null, "Exception loading native libraries: " + e.getLocalizedMessage(), "Exception", JOptionPane.ERROR_MESSAGE);
}
}
As you can see, I basically copied the solution from the first link verbatim, with a few minor modifications just to try and get the application running. I also copied the class from the second link and named it NativeLibraries, the invocation of that method is fairly irrelevant but it shows up in the logs.
Anyway here are the relevant bits of the log output on starting up the application:
2015-07-20 12:32:33 INFO - Attempting to load library ntlmauth.dll
2015-07-20 12:32:33 INFO - Writing dll to: C:\Users\***\lib-bin\ntlmauth.dll
2015-07-20 12:32:33 DEBUG - Loaded libraries: [C:\Program Files\Java\jre1.8.0_45\bin\zip.dll, C:\Program Files\Java\jre1.8.0_45\bin\prism_d3d.dll, C:\Program Files\Java\jre1.8.0_45\bin\prism_sw.dll, C:\Program Files\Java\jre1.8.0_45\bin\msvcr100.dll, C:\Program Files\Java\jre1.8.0_45\bin\glass.dll, C:\Program Files\Java\jre1.8.0_45\bin\net.dll, C:\Users\***\lib-bin\ntlmauth.dll]
2015-07-20 12:32:33 INFO - Application startup
***
2015-07-20 12:32:36 ERROR - Database exception
java.sql.SQLException: I/O Error: SSO Failed: Native SSPI library not loaded. Check the java.library.path system property.
at net.sourceforge.jtds.jdbc.TdsCore.login(TdsCore.java:654) ~[jtds-1.3.1.jar:1.3.1]
at net.sourceforge.jtds.jdbc.JtdsConnection.<init>(JtdsConnection.java:371) ~[jtds-1.3.1.jar:1.3.1]
at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:184) ~[jtds-1.3.1.jar:1.3.1]
at java.sql.DriverManager.getConnection(Unknown Source) ~[na:1.8.0_45]
at java.sql.DriverManager.getConnection(Unknown Source) ~[na:1.8.0_45]
One can see that the library was, indeed, "loaded," from the third line in the log (it's the last entry, if you don't feel like scrolling). However, I simply used the class that I felt like was probably using the native libraries (I also tried the TdsCore class to no avail), as the example that showed how to do this was just using a random class from the package the library was needed in.
Is there something I'm missing here? I'm not very experienced with the JNI or the inner workings of ClassLoaders, so I might just be loading it wrong. Any advice or suggestions would be greatly appreciated!
Welp I figured out a workaround: I ended up using JarClassLoader. This basically entailed copying all my dependencies, both Java and native, into a "libraries" folder within my main .jar, and disabling .jar signing in the IDE. The application is then run by a new class that simply creates a new JarClassLoader object and running the "invokeMain" method--an example is on the website. The whole thing took about three minutes, after several days of banging my head against a wall.
Hope this helps someone someday!

Build jar with tess4j

I create project in Intellij Idea, add tess4j 2.0 from maven, write test application. When I start debug all works fine. When I click "build artifacts" and launch jar file I haven't any result, no errors, nothing.
public class MainApp {
static String fileName = "C:\\Users\\Alex\\Google Drive\\TW\\LIB\\Tess4J\\eurotext.png";
public static void main(String[] args) {
try {
System.setOut(new PrintStream(new File("output-file.txt")));
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Start");
ITesseract instance = new Tesseract1(); // JNA Direct Mapping
try {
String result = instance.doOCR(new File(fileName));
System.out.println(result);
} catch (TesseractException e) {
System.out.println("Error");
System.out.println(e.getMessage());
e.printStackTrace();
}
System.out.println("End");
}
}
Output when debug
Start
The (quick) [brown] {fox} jumps!
Over the $43,456.78 #90 dog
& duck/goose, as 12.5% of E-mail.........
End
Output when launch jar
Start
No "Error", no "End". How it's possible?
I had the same issue. Sometimes it doesn't load classes correctly for some reason. here's how i was able to get it fixed for some far.
Build your artifact and then Remote debug your jar with intellij.
Find out in which line your application breaks.
Then wrap it with try{... line where application breaks..} catch(Error e){e.getMessage(); }
You can see the error message in variable "e" in debug output. So you can determine the issue.
May be this is not an appropriate way to do it. But this is how i was able to track down the errors in my jar file.
How ever it is best to use this as a temporary way to track the errors in your jar and to find out why it breaks and then find out correct way (Which is i'm not currently aware about.. but however got to heard about some library called One-Jar) to build jar without getting any errors on deployment.

JavaPOS printing text throws JposException

I had a lot trouble to setup a applet to work with an Epson TM-T88V pos printer. Now I can send the command for the cutter and it works. But printing any other text is not possible.
The following jpos.JposException occurs:
jpos.JposException: UnicodeDLL:-10An undefined parameter value was set.
at jp.co.epson.upos.T88V.pntr.T88VService.createNormalData(Unknown Source)
at jp.co.epson.upos.core.v1_13_0001.pntr.CommonPrinterService.executeNormalPrint(Unknown Source)
at jp.co.epson.upos.T88V.pntr.T88VService.printNormal(Unknown Source)
at jpos.POSPrinter.printNormal(Unknown Source)
at de.develman.pos.printer.Printer.printReceipt(Printer.java:58)
at de.develman.pos.ui.action.PrintAction.actionPerformed(PrintAction.java:22)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
...
My code looks as follows:
private void initPrinter() throws JposException {
ptr.open("POSPrinter");
ptr.claim(1000);
ptr.setDeviceEnabled(true);
ptr.setMapMode(POSPrinterConst.PTR_MM_METRIC);
}
private boolean printerUseable() throws JposException {
// check if the cover is open
if (ptr.getCoverOpen() == true) {
// cover open so do not attempt printing
System.out.println("printer.getCoverOpen() == true");
return false;
}
// check if the printer is out of paper
if (ptr.getRecEmpty() == true) {
// the printer is out of paper so do not attempt printing
System.out.println("printer.getRecEmpty() == true");
return false;
}
return true;
}
public void printReceipt() {
try {
initPrinter();
if (printerUseable()) {
ptr.printNormal(POSPrinterConst.PTR_S_RECEIPT, "1\n");
ptr.printNormal(POSPrinterConst.PTR_S_RECEIPT, PAPERCUT);
}
} catch (JposException e) {
// display any errors that come up
e.printStackTrace();
} finally {
// close the printer object
try {
ptr.setDeviceEnabled(false);
ptr.release();
ptr.close();
} catch (Exception e) {
}
}
The exceptions points to the line:
ptr.printNormal(POSPrinterConst.PTR_S_RECEIPT, "1\n");
If I run the code from eclipse everything works fine. If I remove the line, the cutter works fine. But if I want to print any text, the given exception is thrown.
What is my problem here?
I have implemented JavaPOS with an Epson TM-T88V printer and I had the same error, but I was able to resolve the error with the sample code from this link:
http://jpos.1045706.n5.nabble.com/file/n2250344/StarReceiptTest.java
Look at the part that starts and terminates a printing transaction.
Hope it helps.
I had the same Problem.
To fix this I have deinstall the Epson JavaPOS-ADK and reinstall it. While reinstalling the ADK again I have checked that I have select the right jpos.xml (that jpos.xml what I also used in my application). After that the error was gone ...
Hope this helps some other guys with the same problem ...
Same problem here, the other answers didn't help for my issue.
I was able to install Epson_JavaPOS_ADK_11120.exe on a Win 10 64 with the steps below, it took me 3 days to figure this out:
remove any version of Java from Control Panel, reboot
install jdk-6u45-windows-i586.exe, reboot
replace java.exe, javaw.exe, javaws.exe in C:\ProgramData\Oracle\Java\javapath with the executable provided by the new Java installation from C:\Program Files\Java\jre6\bin
execute the Epson installer as Administrator
restore your system when done

Problems using Matlab Builder JA

First of all I would like to thank in advance everyone for reading such a long post. I really appreciate your help.
The thing is that I've been doing some research on how to "connect" Matlab and Java for a project I am working on for university. I figured that the most suitable option was using Matlab Builder JA, but I'm having a lot of troubles with it.
I follow step by step the instructions described on a tutorial (the link of the video in below) but get compilation errors over and over, and I really don't know how to fix them. The tutorial is about creating a Java package (demo.jar) with MATLAB ("com.demo"), which contains a class (MLTestClass) with a function makeSqr(n) which returns an n × n square matrix. Then I go to Eclipse, I add to the project both libraries javabuilder.jar and demo.jar and then create the following class:
public class Driver {
public static void main (String[] args) {
MLTestClass x = null;
Object result [] = null;
try {
x = new MLTestClass ();
result = x.makeSqr (1, 5);
System.out.println (result [0]);
} catch (MWException e) {
e.printStackTrace();
}
}
}
Of course I import com.demo.* and com.mathworks.toolbox.javabuilder.*.
Here are the errors the console gives me:
Exception in thread "main" java.lang.ExceptionInInitializerError
at com.mathworks.toolbox.javabuilder.internal.MCRConfiguration.getProxyLibraryDir(MCRConfiguration.java:163)
at com.mathworks.toolbox.javabuilder.internal.MCRConfiguration$MCRRoot.get(MCRConfiguration.java:77)
at com.mathworks.toolbox.javabuilder.internal.MCRConfiguration$MCRRoot.<clinit>(MCRConfiguration.java:87)
at com.mathworks.toolbox.javabuilder.internal.MCRConfiguration.getMCRRoot(MCRConfiguration.java:92)
at com.mathworks.toolbox.javabuilder.internal.MCRConfiguration$ModuleDir.<clinit>(MCRConfiguration.java:66)
at com.mathworks.toolbox.javabuilder.internal.MCRConfiguration.getModuleDir(MCRConfiguration.java:71)
at com.mathworks.toolbox.javabuilder.internal.MWMCR.<clinit>(MWMCR.java:1573)
at com.demo.DemoMCRFactory.(DemoMCRFactory.java:122)
at com.demo.MLTestClass.(MLTestClass.java:63)
at Driver.main(Driver.java:12)
Caused by: java.lang.NullPointerException
at com.mathworks.toolbox.javabuilder.internal.MCRConfiguration$ProxyLibraryDir.get(MCRConfiguration.java:143)
at com.mathworks.toolbox.javabuilder.internal.MCRConfiguration$ProxyLibraryDir.<clinit>(MCRConfiguration.java:158)
... 10 more
Just in case, link tutorial (it's the video): http://www.mathworks.nl/products/javabuilder/description2.html
Anyone has any ideas what the problem could be? It says something about NullPointerException, but I don't know how to solve it as the constructor is provided by the class created with MATLAB. I didn't have any issues installing MCR, and by the way I have MacOS, which I hope is not the source of the problem :).
Again, sorry for the long post and thank you for your time.
Béntor.
Yes, please install MCR. The installation also mentions about setting environmental variables like LD_LIBRARY_PATH etc. If you are using eclipse, i would recommend you update the environmental variables
right click->
properties ->
run/debug settings->
environmental variables
I also had to make sure that variable MCR_CACHE_ROOT pointed to different directory since my home directory was not big enough.
You have install MCR (avaliable in http://www.mathworks.com/products/compiler/mcr/index.html)
None of the above solutions helped me (I already had MCR installed and Macs use DYLD_LIBRARY_PATH instead of LD_LIBRARY_PATH), and noone else online seemed to know. Finally in desperation, I tried editing the DYLD_LIBRARY_PATH and finally got it to work by removing the last part of it: /Applications/MATLAB/MATLAB_Compiler_Runtime/v82/sys/java/jre/maci64/jre/lib
Now the demo application from the tutorial works.
Next comes trying to make my code work.
OS X Paths for Run-Time Deployment
Use these setenv commands to set your MATLAB Runtime paths.
setenv DYLD_LIBRARY_PATH \
mcr_root/version/runtime/maci64 \
mcr_root/version/bin/maci64 \
mcr_root/version/sys/os/maci64
Source: http://www.mathworks.com/help/compiler_sdk/java/mcr-path-settings-for-run-time-deployment.html

Categories