I'm trying to use java.awt.FileDialog in an ImageJ plugin but for some reason I am getting an error that Java cannot find the getFiles method:
C:\File_Opener3.java:50: cannot find symbol symbol : method
getFiles() location: class java.awt.FileDialog fd.getFiles();
^ 1 error
I get a similar error when trying setMultipleMode, but other methods like setVisible and getFile work fine. Can some one tell me what I am doing wrong?
import ij.plugin.*;
import ij.*;
import ij.io.*;
import java.io.*;
import javax.swing.*;
import javax.swing.filechooser.*;
import ij.gui.*;
import ij.plugin.frame.Recorder;
import ij.util.Java2;
import ij.macro.Interpreter;
import java.awt.*;
import java.awt.FileDialog;
import java.awt.Frame;
// Try to figure out why this only allows list veiw
public class File_Opener3 implements PlugIn {
//static File dir;
private static Frame sharedFrame;
private String dir;
private String name;
public void run(String arg) {
openFiles();
IJ.register( File_Opener .class);
}
public void openFiles() {
Frame parent = IJ.getInstance();
if (parent==null) {
if (sharedFrame==null) sharedFrame = new Frame();
parent = sharedFrame;
}
FileDialog fd = new FileDialog(parent, "title"); // From Java.awt.FileDialog
fd.setVisible(true);
//fd.setMultipleMode(true);
name = fd.getFile();
if (name==null) {
if (IJ.isMacOSX())
System.setProperty("apple.awt.fileDialogForDirectories", "false");
Macro.abort();
} else
dir = fd.getDirectory();
//File[] files = fd.getFiles();
fd.getFiles();
//IJ.log("48 fd.getFilenameFilter(): "+fd.getFilenameFilter());
Opener opener = new Opener();
//opener.openMultiple();
/* for (int i=0; i<files.length; i++) {
ImagePlus img = opener.openImage(path, files[i].getName());
if (img!=null)
img.show();
} */
}
}
FileDialog.getFiles() and FileDialog.setMultipleMode() were introduced in Java 1.7. You are probably compiling against an earlier version of Java. If you're using an IDE, check the source level that's set for your project.
Related
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.File;
import login.Login;
import autoitx4java.AutoItX;
import com.jacob.com.LibraryLoader;
class LoginChildB extends Login{
}
public class SeleniumDesktopAppTest {
public static void main(String[] args) throws InterruptedException,AWTException {
final String[] argsCopy = args;
String jacobDllVersionToUse;
if (jvmBitVersion().contains("32")){
jacobDllVersionToUse = "jacob-1.18-x86.dll";
}
else {
jacobDllVersionToUse = "jacob-1.18-x64.dll";
}
Thread workerThread = new Thread(){
public void run(){
LoginChildB.main(argsCopy);
}
};workerThread.start();
File file = new File("lib", jacobDllVersionToUse);
System.setProperty(LibraryLoader.JACOB_DLL_PATH, file.getAbsolutePath());
AutoItX x = new AutoItX();
if(x.winWaitActive("JavaFX Welcome")){
Thread.sleep(1000*1);
x.send("partha"+"\t");
Thread.sleep(1000*1);
x.send("sarathi"+"\t");
Thread.sleep(1000*1);
Robot r = new Robot();
r.keyPress(KeyEvent.VK_ENTER);
r.keyRelease(KeyEvent.VK_ENTER);
}
}
/**
*
* Returns if the JVM is 32 or 64 bit version
*/
public
static String jvmBitVersion(){
return System.getProperty("sun.arch.data.model");
}
}
In the above code I am trying to open a JavaFX form(imported through external jar) and give the inputs to the text fields after that pressing the submit button. When I am running this code in Java 7 I don't have any issues. But when I am running this code using Java 8 the submit button was not getting pressed. Seems java.awt.Robot was not compatible with Java 8. Can any one please tell how to resolve this problem?
I'm fairly new to Selenium webdriver and Java, previously I used Selenium IDE. I'm trying to read testdata from an Excel sheet. Which is then written to Eclipse console, which works, and should be used to execute the actual test, which doesn't work. The actual test is not executed because I get an error argument type mismatch. The code looks like this:
package Test_Excel;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
#RunWith(Parameterized.class)
public class TestWithExcelData {
// Our two parameters
private final int input;
private final int resultExpected;
// Constructor
public TestWithExcelData(int input, int result) {
this.input = input;
this.resultExpected = result;
}
#Parameters
public static Iterable<Object []> data() throws BiffException, IOException
{
String FilePath = "C://Selenium//workspace//testproject//src//testdata//TestData.xls";
FileInputStream fs = new FileInputStream(FilePath);
Object[][] object = new Object[6][2];
Workbook wb = Workbook.getWorkbook(fs);
//locate the excel file in the local machine
Sheet sheet = wb.getSheet("IOResult");
int i=1; //avoid header row
while(!(sheet.getCell(0, i).getContents().equals("end"))) //read data till it reaches the cell whose text is ‘end’
{
object[i-1][0]=sheet.getCell(0, i).getContents();
object[i-1][1]=sheet.getCell(1, i).getContents();
System.out.print(sheet.getCell(0, i).getContents() + "\t");
System.out.print(sheet.getCell(1, i).getContents() + "\t");
System.out.println();
i++;
}
return Arrays.asList(object);
}
#Test
public void testSquareOff(){
Assert.assertEquals(resultExpected, MathUtils.square(input));
}
}
Is there somebody who can point me in the right direction?
Thanks in advance
The method sheet.getCell(column, row).getContents() is returning a String, but your constructor expects int.
You may modify the two lines in the data() method:
object[i-1][0] = Integer.valueOf(sheet.getCell(0, i).getContents());
object[i-1][1] = Integer.valueOf(sheet.getCell(1, i).getContents());
Im trying to use javascript with netbeans. Im supposed to make a mastermind game using javascript. when ı tried to add something to .js ı always have this error;
Exception in thread "main" java.lang.NullPointerException
at java.io.Reader.<init>(Reader.java:78)
at java.io.InputStreamReader.<init>(InputStreamReader.java:72)
at assgn1.Main.main(Main.java:32)
ı couldnt figur out why. thanks for any help.
my codes are;
Main.java
package assgn1;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class Main {
public static void main(String[] args) throws ScriptException {
// TODO code application logic here
// create manager
ScriptEngineManager m = new ScriptEngineManager();
// create javascript script engine
ScriptEngine js = m.getEngineByName("javascript");
// evaluate "hello.js"
InputStream strm = Main.class.getResourceAsStream("/hello.js");
Reader r = new InputStreamReader(strm);
js.eval(r);
}
}
hello.js
importPackage(javax.swing);
importClass(java.lang.System);
function exit(){
System.exit(0);
}
var f= new JFrame("MasterMind");
var b= new JButton("exit");
b.addActionListener(exit);
f.add(path);
f.add(b,"South");
f.setSize(800,800);
f.visible=true;
You should check if
InputStream strm = Main.class.getResourceAsStream("/hello.js");
doesnt return a null object because the path is wrong. Seems like you get the NullPointerException because of that.
I use wicket in my webapplication. I save the Strings in some .properties files as follows:
foo.properties
page.label=dummy
In the html-file, I can acces the String page.label as follows:
index.html
<wicket:message key="page.label">Default label</wicket:message>
Now I wrote some junit test cases for my Application and would like to access the Strings saved in the properties file. My Question is, how to read the String from the properties file?
Try this
import java.io.FileInputStream;
import java.util.Properties;
public class MainClass {
public static void main(String args[]) throws IOException {
Properties p = new Properties();
p.load(new FileInputStream("foo.properties"));
Object label = p.get("page.label");
System.out.println(label);
}
}
This section allow you to read all properties files from wherever you want and load them in the Properties
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
public class MainClass {
private static String PROPERTIES_FILES_PATHNAME = "file:///Users/ftam/Downloads/test/";// for mac
public static void main(String args[]) throws Exception {
Properties p = new Properties();
List<File> files = getFiles();
for(File file : files) {
FileInputStream input = new FileInputStream(file);
p.load(input);
}
String label = (String) p.get("page.label");
System.out.println(label);
}
private static List<File> getFiles() throws IOException, URISyntaxException {
List<File> filesList = new ArrayList<File>();
URL[] url = { new URL(PROPERTIES_FILES_PATHNAME) };
URLClassLoader loader = new URLClassLoader(url);
URL[] urls = loader.getURLs();
File fileMetaInf = new File(urls[0].toURI());
File[] files = fileMetaInf.listFiles();
for(File file : files) {
if(!file.isDirectory() && file.getName().endsWith(".properties")) {
filesList.add(file);
}
}
return filesList;
}
}
Wicket has its own way of localizing the resource, taking into account the component tree. See the javadoc for the StringResourceLoader.
One way of loading the Resource would be:
WicketTester tester = new WicketTester(new MyApplication());
tester.startPage(MyPage.class);
Localizer localizer = tester.getApplication().getResourceSettings()
.getLocalizer();
String foo = localizer.getString("page.label",tester.getLastRenderedPage(), "")
Using Apache Commons Configuration is a pretty good choice!
You can use load and then get("page.label")
Have this field inside your class:
import java.util.ResourceBundle;
private static ResourceBundle settings = ResourceBundle.getBundle("test",Locale.getDefault());
then a test.properties file like this:
com.some.name=someValueHere
Finally you can access the property values this way:
private String fieldName = settings.getString("com.some.name");
hello:
I'm writing code in java for nutch(open source search engine) to remove the movments from arabic words in the indexer.
I don't know what is the error in it.
Tthis is the code:
package com.mycompany.nutch.indexing;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.Text;
import org.apache.log4j.Logger;
import org.apache.nutch.crawl.CrawlDatum;
import org.apache.nutch.crawl.Inlinks;
import org.apache.nutch.indexer.IndexingException;
import org.apache.nutch.indexer.IndexingFilter;
import org.apache.nutch.indexer.NutchDocument;
import org.apache.nutch.parse.getData().parse.getData();
public class InvalidUrlIndexFilter implements IndexingFilter {
private static final Logger LOGGER =
Logger.getLogger(InvalidUrlIndexFilter.class);
private Configuration conf;
public void addIndexBackendOptions(Configuration conf) {
// NOOP
return;
}
public NutchDocument filter(NutchDocument doc, Parse parse, Text url,
CrawlDatum datum, Inlinks inlinks) throws IndexingException {
if (url == null) {
return null;
}
char[] parse.getData() = input.trim().toCharArray();
for(int p=0;p<parse.getData().length;p++)
if(!(parse.getData()[p]=='َ'||parse.getData()[p]=='ً'||parse.getData()[p]=='ُ'||parse.getData()[p]=='ِ'||parse.getData()[p]=='ٍ'||parse.getData()[p]=='ٌ' ||parse.getData()[p]=='ّ'||parse.getData()[p]=='ْ' ||parse.getData()[p]=='"' ))
new String.append(parse.getData()[p]);
return doc;
}
public Configuration getConf() {
return conf;
}
public void setConf(Configuration conf) {
this.conf = conf;
}
}
I think that the error is in using parse.getdata() but I don't know what I should use instead of it?
The line
char[] parse.getData() = input.trim().toCharArray();
will give you a compile error because the left hand side is not a variable. Please replace parse.getData() by a unique variable name (e.g. parsedData) in this line and the following lines.
Second the import of
import org.apache.nutch.parse.getData().parse.getData();
will also fail. Looks a lot like a text replace issue.