Java: Illegal start of expression - java

I'm just learning Java and wish to use the Path object:
Path file = ...;
And it's giving me: "Illegal start of expression"
I have the following imports:
import java.nio.file.*;
import java.nio.file.attribute.*;
I am running JDK 1.7 platform (JDK 7) according to NetBeans. Have googled to the end of earth and cannot find squat on this error.
I'm assuming path file = ...; is some new syntax or feature that my current JDK isn't recognizing???
EDIT |
import javax.swing.*;
import java.nio.file.*;
import java.nio.file.attribute.*;
public class MainWindow extends JFrame {
public MainWindow()
{
initComponents();
}
private void cboModelFocusGained(java.awt.event.FocusEvent evt)
{
Path file = ...;
}
}

Path file = ...;
is not a valid statement, in any version of Java. My guess is that you copied and pasted this from some web site, but the three dots were just meant as an ellipsis meaning "the initialization code must go here".
What do you think these three dots mean?

The ... are place holders, these should be replaced with the actual path to the file on your computer. For example if the file exists in C:\Directory\file.txt, then the code should be:
Path file = "C:\\Directory\\file.txt";

Related

Using VC++ dll using JNA

I am using a BTICARD.DLL, which is the dll of Arinc429 card. I need to write wrapper class in Java for the functions like BTICard_CardOpen for example.
I Had written an interface below BTICardAPI.java:
package NLIPjt;
import com.sun.jna.win32.StdCallLibrary;
import com.sun.jna.Native;
// import com.sun.jna.ptr.IntByReference;
import com.sun.jna.Pointer;
public interface BTICardAPI extends StdCallLibrary {
BTICardAPI INSTANCE = (BTICardAPI) Native.loadLibrary("BTICARD", BTICardAPI.class);
int BTICard_CardOpen(Pointer LPHCARD, int cardnum);
}
and my Java implementation prog
BTICardTest.java:
package NLIPjt;
// import com.sun.jna.ptr.IntByReference;
import com.sun.jna.Pointer;
public class BTICardTest {
public static void main(String args[]) {
BTICardAPI BTI1 = BTICardAPI.INSTANCE;
int iErr;
int CardNo = 0;
Pointer CardHandle = null;
iErr = BTI1.BTICard_CardOpen(CardHandle, CardNo);
System.out.println("Error Value: " + iErr);
}
}
i get the following error in netbeans IDE:
Exception in thread "main" java.lang.UnsatisfiedLinkError: Error looking up function 'BTICard_CardOpen': The specified procedure could not be found.
at com.sun.jna.Function.<init>(Function.java:245)
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:566)
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:542)
at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:528)
at com.sun.jna.Library$Handler.invoke(Library.java:228)
at com.sun.proxy.$Proxy0.BTICard_CardOpen(Unknown Source)
at NLIPjt.BTICardTest.main(BTICardTest.java:14)
Looking for a solution!!
According to the documentation you need to make the library available. There are three ways to do this.
Make your target library available to your Java program. There are
several ways to do this:
The preferred method is to set the jna.library.path system property to
the path to your target library. This property is similar to
java.library.path, but only applies to libraries loaded by JNA.
Change the appropriate library access environment variable before
launching the VM. This is PATH on Windows, LD_LIBRARY_PATH on Linux,
and DYLD_LIBRARY_PATH on OSX.
Make your native library available on your classpath, under the path
{OS}-{ARCH}/{LIBRARY}, where {OS}-{ARCH} is JNA's canonical prefix for
native libraries (e.g. win32-x86, linux-amd64, or darwin). If the
resource is within a jar file it will be automatically extracted when
loaded.

JMeter Bean Shell Sampler error "...Static method get( java.lang.String ) not found in class'java.nio.file.Paths" when copying files

I am attempting to copy and rename a file on my local machine (Win 7) using Bean Shell Sampler in JMeter 3.0 (Java v1.8). The idea is to create the new file with a unique name and have the name saved as a variable that can be used in place of the file name in an FTP PUT request.
Here is the code I am using for the copy and rename:
import java.text.*;
import java.nio.file.StandardCopyOption.*;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
String filename = new SimpleDateFormat("dd-MM-yyyy_hh:mm:ss").format(new Date())+".xlsx";
log.info(filename);
Path source = Paths.get("C:/dropfile/qatp/QATP_GuestRecords.xlsx");
Path target = Paths.get("C:/dropfile/qatp/"+filename);
Files.copy(source, target, REPLACE_EXISTING);
The error I am receiving in the log:
ERROR - jmeter.util.BeanShellInterpreter: Error invoking bsh method:
eval Sourced file: inline evaluation of: ``import java.text.; import
java.nio.file.StandardCopyOption.; import java.io.IO . . . '' : Typed
variable declaration : Error in method invocation: Static method get(
java.lang.String ) not found in class'java.nio.file.Paths'
I have been searching for an answer to this issue and came across a solution where the suggestion was:
"My guess is that the problem is that it's not populating the varargs parameter. Try:
Path target = Paths.get(filename, new String[0]);"
I tried this solution by modifying my code like so:
import java.text.*;
import java.nio.file.StandardCopyOption.*;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
String filename = new SimpleDateFormat("dd-MM-yyyy_hh:mm:ss").format(new Date())+".xlsx";
log.info(filename);
Path source = Paths.get("C:/dropfile/qatp/QATP_GuestRecords.xlsx", new String[0]);
Path target = Paths.get("C:/dropfile/qatp/"+filename, new String[0]);
Files.copy(source, target, REPLACE_EXISTING);
And received this error:
ERROR - jmeter.util.BeanShellInterpreter: Error invoking bsh method:
eval Sourced file: inline evaluation of: ``import java.text.; import
java.nio.file.StandardCopyOption.; import java.io.IO . . . '' : Typed
variable declaration : Method Invocation Paths.get
Does anyone know why I am hitting this error and how to get around it?
Even in plain old Java this is a misleading use of Paths.get, which takes an URI, or an array of strings (varargs). See javadoc.
In Java what you tried works because the static typing allow the compiler to determine that you are passing an array of a single String. Apparently BeanShell does not and gets confused. The trick suggested in the other answer is not a good one in my opinion: again in Java it would work, by joining the two strings (2nd one is empty, so result is 1st string, which is what you want), but it confuses BeanShell all the same because there is another static get method that takes 2 arguments.
If you already have the path as a single String, try this instead:
Path source = new File("C:/dropfile/qatp/QATP_GuestRecords.xlsx").toPath();
Alternatively, you could use Paths.get like this:
Path source = Paths.get("C:", "dropfile", "qatp", "QATP_GuestRecords.xlsx");
Or like this (varargs is syntaxic sugar to help pass an array):
Path source = Paths.get(new String [] { "C:/dropfile/qatp/QATP_GuestRecords.xlsx" });
It's perfectly valid to pass fragments of path as arguments, or the entire path string as single argument, but that seems to trip BeanShell, so, better avoid Paths.get in BeanShell, unless you pass an array explicitly as in last example.
Beanshell != Java, it doesn't support all the Java features (think about it as about Java 1.5 and amend your code appropriately.
So I would recommend switching to JSR223 Sampler and Groovy language, Groovy is much more Java-compliant and performs much better.
Also be aware that you can use FileUtils.copyFile() method which will work for both Beanshell and/or Groovy
import org.apache.commons.io.FileUtils;
import java.text.SimpleDateFormat;
String filename = new SimpleDateFormat("dd-MM-yyyy_hh:mm:ss").format(new Date()) + ".xlsx";
FileUtils.copyFile(new File("/tmp/QATP_GuestRecords.xlsx"), new File("/tmp/" + filename));
See Groovy is the New Black article for more information on using Groovy language in JMeter test scripts.

Calling A Java Library From Pentaho Kettle

I have developed a java class exported into a .jar library that will be called by a Pentaho Kettle 'modified java script'. The .jar is compiled in Eclipse with JDC Compliance level 1.7.
When I try to use this class inside a 'modified java script', I get the error: ReferenceError: “xeroCallPackage” is not defined. I have tried lots of things without much luck so far.
My file xeroCallPackage.jar is in the path with the other *.jar files in Pentaho (..\data-integration\lib)
For info:
The stripped down (for simplicity) java library code is here:
package xeroCallPackage;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Collections;
import java.util.Map;
public class xeroURLCall {
public String getResponse(String CONSUMER_KEY, String CONSUMER_PRIVATE_KEY, String URL) throws IOException, OAuthException, URISyntaxException {
// stripped out code here
return response.readBodyAsString();
}
}
The stripped down Pentaho 'modified java script' is here:
var CONSUMER_KEY = "ffffff";
var CONSUMER_PRIVATE_KEY = "aaaaa";
var URL = "https://gggggggg.rrrrr.wwww";
var ResponseAsString;
ResponseAsString = new xeroCallPackage.xeroURLCall.getResponse(CONSUMER_KEY,CONSUMER_PRIVATE_KEY,URL);
You will either have to have org as top-most package or prefix the fully qualified name of your class with Packages.
So in your case the fully qualified name of your class that can be used for calling from Spoon will be Packages.xeroCallPackage.xeroURLCall
Apart from that the supplied JavaScript code won't work (but maybe that's just because of the stripped down code). You'd have to create a xeroURLCall object first and then call the getResponse method on that object:
var call = new Packages.xeroCallPackage.xeroURLCall(...);
var responseAsString = call.getResponse(CONSUMER_KEY,CONSUMER_PRIVATE_KEY,URL);

Exception in thread "main" java.lang.UnsatisfiedLinkError: jnidispatch (/com/sun /jna/win32-x86/jnidispatch.dll) not found in resource path

I have a small test program which runs perfectly in the JBuilder 6 debugger. When I make a .jar file and run it I get an error
>java -jar testadll.jar
Start of DLL test
Exception in thread "main" java.lang.UnsatisfiedLinkError: jnidispatch (/com/sun
/jna/win32-x86/jnidispatch.dll) not found in resource path
at com.sun.jna.Native.loadNativeLibraryFromJar(Native.java:708)
at com.sun.jna.Native.loadNativeLibrary(Native.java:685)
at com.sun.jna.Native.<clinit>(Native.java:109)
at testadll.TestThisDLL$PenniesLib.<clinit>(TestThisDLL.java:24)
at testadll.TestThisDLL.main(TestThisDLL.java:33)
I have searched my drive and there is no jnidispatch.dll on it.
The program is
package testadll;
import com.sun.jna.Library;
import com.sun.jna.Native;
//import com.sun.jna.NativeLong;
import com.sun.jna.Platform;
import com.sun.jna.win32.StdCallLibrary;
//import com.sun.jna.*;
public class TestThisDLL {
public interface PenniesLib extends StdCallLibrary {
PenniesLib INSTANCE = (PenniesLib) Native.loadLibrary(
"PenniesLib", PenniesLib.class);
int a();
}
public static void main( String args[] ) {
System.out.println("Start of DLL test");
//TestDLL t = new TestDLL();
//System.out.println("DLL loaded");
int value = PenniesLib.INSTANCE.a();
System.out.println("DLL response is " + String.valueOf(value));
}
}
You've apparently merged JNA's classes with your own jar file, but omitted its native support. Ensure that all files from the original jna.jar (not just class files) are copied to the new destination and that their original paths are preserved.
Specifically, your jar file must include com/sun/jna/win32-x86/jnidispatch.dll. If you want to include support for other platforms, you must include com/sun/jna/*/jnidispatch as well.
You should use a version of jna.jar, that supports 64 bit, for example
jna-4.1.0.jar or jna-3.4.0.jar.

Can't use jar library in my java file

Sorry for this noobie question, I'm new to Java, and instead of using IDE, i want to using command line to learn what's running under the hood
I'm following the Getting Started guild on MigLayout
#MigWindow.java
public class MigWindow {
public static void main(){
javax.swing.JPanel panel = new javax.swing.JPanel(new MigLayout());// a simple line to make sure the library jar import correctly
}
}
and compile with these command:
javac -cp ./MigLayout.jar MigWindow.java
and I got a error:
MigWindow.java:3: cannot find symbol
symbol : class MigLayout
location: class MigWindow
javax.swing.JPanel panel = new javax.swing.JPanel(new MigLayout());
^
1 error
It seems the jar library doesn't import correctly, any idea?
~
Make sure you add the import for MigLayout
import net.miginfocom.swing.MigLayout;
It may sound obvious, but make sure MigLayout.jar the current directory when calling javac here and that your JAR file has not been corrupted.
Update:
To check that your JAR file does contain the class you can do:
jar tvf MigLayout.jar
and check for the MigLayout class. Failing to find the class you can download the correct one from here.
You are missing an import statement in your Source File. The compiler does not know where 'MigLayout' is coming from.
Add at the top of your file, but below of your package statement (if any) an import, e.g.
import package.MigLayout;
This tells the compiler what to import from the given class path. You will need to replace package with the correct package.

Categories

Resources