I downloaded the sources from here. This is the code with wich I am trying to open com port 13.
I saw in the device menager that com port13 is present.
import jssc.*;
public class Main {
static boolean s = false;
public static void main(String[] args) {
// TODO Auto-generated method stub
SerialPort ser = new SerialPort("COM1");
try {
s = ser.openPort();
} catch (SerialPortException e) {
// TODO Auto-generated catch block
System.out.println("Riko Stana neshto");
//e.printStackTrace();
}
//System.out.println("Riko" + ser.getPortName() );
}
}
When ran in Eclipse this appears in the console:
Exception in thread "main" java.lang.UnsatisfiedLinkError: jssc.SerialNativeInterface.openPort(Ljava/lang/String;Z)J
at jssc.SerialNativeInterface.openPort(Native Method)
at jssc.SerialPort.openPort(SerialPort.java:158)
at Main.main(Main.java:9)
The linbrary consist of java files and two dll file. I linked the java files to my eclipse project, but not sure how they are linked to the dll files. Should I do something in order to link the .java files to the .dll files? Could it be the cause of my problem? Any help is very much appreciated.
Download the JSSC zip here: https://code.google.com/archive/p/java-simple-serial-connector/downloads
Extract jssc.jar in your project folder.
Add the "jssc.jar" to your classpath as a .jar library.
Optional: Extract javadoc/jssc-2.7.0-javadoc.jar and javadoc/jssc-2.7.0-src.jar in your project folder and add them as javadoc and sources respectively.
Problem solved.
Edit - pitcure:
Related
I am writing sikuli scripts in Eclipse..
I have added sikulixapi-1.1.0.jar and sikuli-3.0.0 in Eclipse Project build path.
I am getting following error:
*** classpath dump end
[error] RunTimeINIT: *** terminating: libs to export not found on above classpath: /sikulixlibs/windows/libs64
Picked up _JAVA_OPTIONS: -Xmx512M
I have added folderpath where the jar is there, i have added in Environment Variable as Path
How to resolve this issue?
Here is my code..
import org.sikuli.script.Button;
import org.sikuli.script.FindFailed;
import org.sikuli.script.Key;
import org.sikuli.script.Match;
import org.sikuli.script.Screen;
import org.sikuli.script.Keys;
public class test {
/**
* #param args
* #throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
try{
Screen s = new Screen();
s.click("C:\\Users\\eguru\\Desktop\\sikuli\\step1.png");
s.click("C:\\Users\\eguru\\Desktop\\sikuli\\step2.png");
s.click("C:\\Users\\eguru\\Desktop\\sikuli\\field38.png");
Thread.sleep(2000);
s.type("D");
s.type(Key.TAB);
if(s.exists("C:\\Users\\eguru\\Desktop\\sikuli\\warningOkbutton.png")!=null){
s.click("C:\\Users\\eguru\\Desktop\\sikuli\\warningOkbutton.png");
}
s.click("C:\\Users\\eguru\\Desktop\\sikuli\\step3.png");
s.click("C:\\Users\\eguru\\Desktop\\sikuli\\lastnametextfield.png");
s.type("a",Key.CTRL);
s.type(Key.DELETE);
s.type("Last Name for CTR");
s.click("C:\\Users\\eguru\\Desktop\\sikuli\\firstnametextfield.png");
s.type("a",Key.CTRL);
s.type(Key.DELETE);
s.type("First Name for CTR");
s.click("C:\\Users\\eguru\\Desktop\\sikuli\\step4.png");
s.click("C:\\Users\\eguru\\Desktop\\sikuli\\step0.png");
String typeoffiling = "C:\\Users\\eguru\\Desktop\\sikuli\\typeoffilingcheckbox.png";
while(s.exists(typeoffiling)== null){
s.wheel(Button.WHEEL_DOWN, 5);
}
s.click(typeoffiling);
//s.click("C:\\Users\\eguru\\Desktop\\sikuli\\typeoffilingcheckbox.png");
Thread.sleep(10000);
String saveButton = "C:\\Users\\eguru\\Desktop\\sikuli\\savebutton.png";
while(s.exists(saveButton)== null){
s.wheel(Button.WHEEL_DOWN, 5);
}
s.click(saveButton);
}
catch(FindFailed e){
e.printStackTrace();
}
}
}
You have to double click the .jar.
There's a setup over there. I selected pack 2 and 3.
1.Download this jar file, https://mvnrepository.com/artifact/org.sikuli/SikuliX-Setup/1.1.0
Double click and select your options
I will generate sikulixapi.jar and sikulilibswin-1.1.0
link those jars with java build path -> classpath.
Ctrl+B build all
I hope your issues has been solved.
For people who do not have the solution of our partner alansiqueira27 (that my example), they should be downloaded directly,
sikulixlibswin-1.1.1.jar (last version windows)
with these lib should not have problems in windows and that is the link:
https://jar-download.com/cache_jars/com.sikulix/sikulixlibswin/1.1.1/jar_files.zip
I have been searching for a long time to find a way to import a config file on an Android app using Appium with Java. I am still learning on this and I hope that I find some help here.
Here is the case:
I have an android app that I started testing but before that I have to import a configuration file into this app in order to become active.
public class sipphone extends base {
public static void main(String[] args) throws MalformedURLException {
// TODO Auto-generated method stub
AndroidDriver<AndroidElement> driver=capabilities();
driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
File configImport= new File("src");
File file= new File(configImport, "testconfig.config");
driver.findElementById("android:id/button1").click();
driver.findElementById("android:id/btn_later").click();
try{
//import file from src directory
}
catch (Exception e) {
return;
}
}
}
How can I load the file inside the try block and then into the Android App?
Please check
https://appium.github.io/java-client/io/appium/java_client/android/PushesFiles.html#pushFile
Search on http://discuss.appium.io/ or https://github.com/appium/appium on how to use it
I am trying to move directory forcefully that means if already exist then overwrite without asking.
Code :
import java.io.IOException;
import java.lang.System;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
public class MoveDirectory {
private static void movefilesandfolder(String sourceFilevar,String destinationFilevar)
{
System.out.println("source="+sourceFilevar);
System.out.println("destination="+destinationFilevar);
Path sourceFile=Paths.get(sourceFilevar);
Path destinationFile=Paths.get(destinationFilevar);
try {
Files.move(sourceFile, destinationFile,StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Sole entry point to the class and application.
* #param args Array of String arguments.
*/
public static void main(String[] args) {
movefilesandfolder("C:\\FTPDownload\\Downloaded\\epi141225_0001","C:\\FTPDownload\\In_Progress\\epi141225_0001");
}
}
Error output:
source=C:\FTPDownload\Downloaded\epi141225_0001
destination=C:\FTPDownload\In_Progress\epi141225_0001
java.nio.file.DirectoryNotEmptyException: C:\FTPDownload\In_Progress\epi141225_0001
at sun.nio.fs.WindowsFileCopy.move(WindowsFileCopy.java:372)
at sun.nio.fs.WindowsFileSystemProvider.move(WindowsFileSystemProvider.java:286)
at java.nio.file.Files.move(Files.java:1345)
at MoveDirectory.movefilesandfolder(MoveDirectory.java:22)
at MoveDirectory.main(MoveDirectory.java:36)
This code works if Folder not present on destination path but fails if already exist. This code doesn't work even if empty folder is present on destination path.
Files.move(sourceFile, destinationFile,StandardCopyOption.REPLACE_EXISTING);
In this call option i am using is not working for folder. This code Tested for file and it worked for already exist file as well.
But I want to move/overwrite folder.
You can delete destination directory before moving, or, if you want to merge directories together, loop your directory files and move anyone in new folder
System.out.println("source="+sourceFilevar);
System.out.println("destination="+destinationFilevar);
Path sourceFile=Paths.get(sourceFilevar);
Path destinationFile=Paths.get(destinationFilevar);
try {
if(new File(destinationFile).exists()){
// DELETE DIRECTORY
}
Files.move(sourceFile, destinationFile,StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
or
System.out.println("source="+sourceFilevar);
System.out.println("destination="+destinationFilevar);
Path sourceFile=Paths.get(sourceFilevar);
Path destinationFile=Paths.get(destinationFilevar);
try {
if(new File(destinationFile).exists()){
// for each file in sourceFile
// Files.move file ...
}else{
Files.move(sourceFile, destinationFile,StandardCopyOption.REPLACE_EXISTING);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
javadoc:
public static Path move(Path source,
Path target,
CopyOption... options)
throws IOException
Move or rename a file to a target file.
By default, this method attempts to move the file to the target file, failing if the target file exists except if the source and target are the same file, in which case this method has no effect. If the file is a symbolic link then the symbolic link itself, not the target of the link, is moved. This method may be invoked to move an empty directory.
You can try using Path.resolve(other Path) method like this
Files.move(sourceFile, destinationFile.resolve(Paths.get(sourceFile).getFileName()), StandardCopyOption.REPLACE_EXISTING)
Java docs, give the best explanation about this.
i have one problem in which i need some help.
Problem statement:
I'm using one jar to generate reports in excel sheet format. This jar is required only if the user wants to generate report in excel format. Other formats of report available are html and txt which don't require this jar.
The current user generates the reports in html format so he says, why should I download this jar and export it in the classpath when I don't need report in the excel format.
Now the problem is if this jar is removed, this build will fail/as all the imports to the classes which are being used will give error. Class.forName can load the class at run-time and doesn't give me error but with this I will not be able to use the method of that class since I cannot have the reference of the class.
Is there any way out or this is not possible?
Did you try to compile it with the jar as a dependency for the compile.
Then at runtime, you will have a part where you check if the jar is needed and if so you can dynamically get the jar and load it like so (Code does not work like this of course ;) ):
import java.lang.reflect.Method;
import java.net.URLClassLoader;
Method addURL = null;
try {
addURL = URLClassLoader.class.getDeclaredMethod("addURL",
new Class[]{URL.class});
} catch (Exception e1) {
//Log error
}
addURL.setAccessible(true);
//Maybe download the file or check if file exist else give out error and end processing
File yourJar = new File(filePath+"/"+fileName+".jar");
//Replace Your.Main.Class with your main class
addURL.invoke(Your.Main.Class.class
.getClassLoader(), yourJar.toURI().toURL());
// Your class should now be loaded and no more ClassNotFound exception should occur when it is accessed, but not if it is accessed before!
The problem is that you are hard wiring your dependencies. So your code needs to do some imports for the third party libs. What you need is to loosely couple the third party libs so that the core of you application does not need to import anything related to 3rd party libs. Use an interface which defines a method or the set of methods needed to generate reports in any format. Make this interface part of your core application. Format specific implementation goes then in separate modules which are dependent on your core application and on the 3rd party libs. Use a factory in the core application to load the specific implementation at runtime using refelction. If a format is requested from which the relevant module jars are not present in the classpath, a ClassNotFoundException will be thrown, catch it and handle accordingly.
Here a sample structure for your application
Core application
class ReportData {
}
interface ReportGenerator {
byte[] generate(ReportData data);
}
class ReportGeneratorFactory {
public ReportGenerator getInstance(String format)
throws InstantiationException, IllegalAccessException, ClassNotFoundException {
ReportGenerator reportGenerator = null;
if("txt".equals(format)) {
reportGenerator = (ReportGenerator)
Class.forName("com.foo.TxtReportGenerator").newInstance();
} else if("html".equals(format)) {
reportGenerator = (ReportGenerator)
Class.forName("com.foo.HtmlReportGenerator").newInstance();
} else if("xl".equals(format)) {
reportGenerator = (ReportGenerator)
Class.forName("com.foo.XlReportGenerator").newInstance();
} else {
throw new UnsupportedOperationException(
String.format("Unsupport format %s", format));
}
return reportGenerator;
}
}
Txt / Html Export (Could be part of the core application if no 3rd party lib are needed)
class TxtReportGenerator implements ReportGenerator {
public byte[] generate(ReportData data) {
// TODO Auto-generated method stub
return null;
}
}
class HtmlReportGenerator implements ReportGenerator {
public byte[] generate(ReportData data) {
// TODO Auto-generated method stub
return null;
}
}
Module (own jar) for XL report (depends on your core application and on the 3rd party lib)
class XlReportGenerator implements ReportGenerator {
public byte[] generate(ReportData data) {
// TODO Auto-generated method stub
return null;
}
}
Usage:
public static void main(String[] args)
throws InstantiationException, IllegalAccessException, ClassNotFoundException {
byte[] report = new ReportGeneratorFactory()
.getInstance("xl")
.generate(new ReportData());
}
I've tried this code and added the needed jar files but still I'm getting an error message like Exception in thread "main" java.lang.UnsatisfiedLinkError: Unable to load library 'libtesseract302'.
Is there a complete tutorial how to extract text and what things should be done to address the error? Any help is appreciated...
import net.sourceforge.tess4j.*;
import java.io.File;
public class ExtractTxtFromImg {
public static void main(String[] args) {
File imgFile = new File("C:\\Documents and Settings\\rueca\\Desktop\\sampleImg.jpg");
Tesseract instance = Tesseract.getInstance(); // JNA Interface Mapping
// Tesseract1 instance = new Tesseract1(); // JNA Direct Mapping
try {
String result = instance.doOCR(imgFile);
System.out.println(result);
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}
In addition to adding the jars, you also need to add the natives. You can do so with Djava.library.path="C:\[absolute path to dir containing *.dll files and such]"
Note that you need to provide the directory, not the file itself.