Illegal start of expression error in java swing auto-generated code - java

I am using Java Swing to create a GUI.
import javax.swing.*;
import javax.swing.filechooser.*;
import javax.swing.InputVerifier;
import java.lang.Process;
import java.lang.ProcessBuilder;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
It is telling me that the line " private void outputDirActionPerformed..." has an illegal start of expression, however that line of code is autogenerated by NetBeans. commandPLINK is an array of parameters, and is behaving correctly.
ProcessBuilder pb = new ProcessBuilder(commandPLINK);
try {
pb.inheritIO();
Process p = pb.start();
} catch (IOException ex) {
Logger.getLogger(rtPCRGui.class.getName()).log(Level.SEVERE, null, ex);
}
} //convertButtonActionPerformed
private void outputDirActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
Any idea what might be happening that's causing the autogenerated code to throw this error?

This error happens when you are missing a } somewhere before the line that triggers the error. You should look at the preceding method and see if every { is matched with a corresponding }.
Also, you may look if there is an extra } after the generated method.
To be more clear, this error means that the compiler did not expect to have a function definition starting there, because it is believing that you are still in a method block, and a method can not be inside another method.

Related

Why won't createNewFile() recognize it's import?

I made this class in a NetBeans project and cannot figure out why the createNewFile method will not recognize it's import. NetBeans is giving the "cannot find symbol" error for that line. "createNewFile" is the only part underlined in red on that line. It also gives warning on "import java.io.File" saying that it is never used.
I've added try and catch blocks around the method but they make no difference. Got rid of them in the example below for the sake of simplicity.
import java.util.Scanner;
import java.io.File;
public class Bleh {
Scanner in = new Scanner(System.in);
User u = new User();
public void setUserName() {
System.out.print("Name: ");
u.setName(in.nextLine());
}
public void checkForAccount() {
createNewFile(u.getName());
}
}
Your import of java.io.File imports exactly that, the class itself, into your namespace. It doesn't import all of File's methods, and even if it did, there is no such method File.createNewFile(String); you need to create a File object and call the method on it:
new File(u.getName()).createNewFile();

Nashorn java.lang.NoClassDefFoundError: jdk/nashorn/api/scripting/JSObject

I am migrating my Eclipse RCP to use JDK 8 and I heavily use the JS ScriptEngine. Now that Nashorn is introduced I had to add the following line to get the importClass and importPackage functions to work:
load("nashorn:mozilla_compat.js");
After doing so, I got java.lang.NoClassDefFoundError: jdk/nashorn/api/scripting/JSObject.
I am using Nashorn inside an Eclipse RCP. The problem occurs when I call a Java function from the Javascript and try to use the parameter sent. The parameter I want to send is a Javascript function that I would like to execute call on later in the code.
I have the following code:
TestNashorn.java
package com.test.nashorn;
import java.io.FileNotFoundException;
import java.io.FileReader;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.script.Invocable;
import jdk.nashorn.api.scripting.JSObject;
public class TestNashorn {
public static void main(String[] args) {
ScriptEngine engine = (new ScriptEngineManager()).getEngineByName("js");
try {
engine.eval(new FileReader("C:/Users/user/workspace_nashorn/TestNashorn/src/com/test/nashorn/test.js"));
Object o = ((Invocable)engine).invokeFunction("generate");
} catch (ScriptException | FileNotFoundException | NoSuchMethodException e) {
e.printStackTrace();
}
}
public static int test(JSObject o1) {
System.out.println(o1.getClass().toString());
JSObject som = ((JSObject)o1);
return 1;
}
}
test.js
load("nashorn:mozilla_compat.js");
importClass(com.test.nashorn.TestNashorn);
function generate()
{
function asd(variablex) { print('Hello, ' + variablex); }
var result = TestNashorn.test(asd);
}
The problem occurs in line JSObject som = ((JSObject)o1);, although I can successfully import jdk.nashorn.api.scripting.JSObject;.
The exception message exactly says:
jdk.nashorn.api.scripting.JSObject cannot be found by com.test.nashorn_1.0.0.qualifier
So.. I got to fix my issue and was able to use JSObject in my code. What I have done was the following:
Added -Dorg.osgi.framework.bundle.parent=ext to myproduct.product file
This added it to the .ini file in my product build which revealed the classes found in Nashorn APIs.

Doing an action after a set delay in java

Edit: Got it to work. My problem was I was using cmd to compile which exited the vm before the delay ended. Switched to jGrasp and the program worked as intended. Next I need to learn how to actually make a java applet to properly run on my computer. Thanks for your help everyone
I'm trying to set an alarm of sorts using java. I'd like to open a webpage after a set delay. The code below compiles and runs without errors or warnings but running the code does nothing. Just starts and stops the program. I have a feeling the issue arises from how I catch the exceptions but I'm not sure. I also am a little lost on what the actionPerformed() method does. Any help or insight is greatly appreciated
import java.awt.Desktop;
import java.net.URI;
import java.net.URISyntaxException;
import java.io.IOException;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.Timer;
public class YtAlarmTest
{
public static void main (String [] args)
{
String url = "https://stackoverflow.com/questions/ask";
int delay = 1000;
ActionListener task = new ActionListener()
{
public void actionPerformed(ActionEvent evt)
{
try
{
if (Desktop.isDesktopSupported())
{
Desktop.getDesktop().browse(new URI(url));
}
}
catch (URISyntaxException e)
{
System.out.println("exception");
}
catch (IOException e)
{
System.out.println("exceptio");
}
}
};
new Timer(delay, task).start();
}
}

How to catch all links that are in Windows clipboard?

I would realize a method for catch all links that are in Windows clipboard when i select and copy a text HTML, but i do not found any example to realize it.
I'm already know how to catch string from clipboard but when try to print it (or paste it), i lost formatting (and relative href).
Any idea?
#VGR: I found your answer very helpful. I use it and i have create this class that copy all HTML data. Now i make/search a parser method to catch links and problem is solved.
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
public class main
{
public static void main(String[] args)
{
Clipboard clipboard=Toolkit.getDefaultToolkit().getSystemClipboard();
DataFlavor df=DataFlavor.allHtmlFlavor;
try
{
System.out.println("HTML of selected text="+clipboard.getData(df));
}
catch(UnsupportedFlavorException|IOException exception)
{
exception.printStackTrace();
}
}
}

740: The requested operation requires elevation in java

I tried this java code, in this code i move a file from from one directory to another one,
then execute the file. I am using Windows 7 OS.
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import org.apache.commons.io.FileUtils;
import org.omg.CORBA.Environment;
public class JFileChooserTest {
public static void main(String[] args) {
String filelocation="C:\\Users\\FSSD\\Desktop\\OutPut\\Target\\setup.exe";
File trgDir = new File(filelocation);
System.err
.println("file location>>>>>>>>>>>>>>>>>>>"
+ filelocation);
File desDir = new File(
"C:\\Users\\FSSD\\IndigoWorkSpace\\Swing\\test");
try {
FileUtils.copyFileToDirectory(trgDir, desDir);
// FileUtils.copyDirectory(srcDir, trgDir);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Runtime rt=Runtime.getRuntime();
try {
Process p=rt.exec("runas /user:FSSD test/setup.exe");
//Process p= rt.exec("test/setup.exe");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
While i execute this i got "740: The requested operation requires elevation " error , if any possibilities to resolve it.
Short answer is that you can't do it in-process. You need to launch a new process that is elevated at the command line. (see the elevate command).
Please see this question and answer here - they address your issue.
Service host local system network restricted 10
HKEY_LOCAL_MACHINE > SYSTEM > ControlSet001
registry value named Start in the right hand panel and double click on it.
Link

Categories

Resources