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
Related
A bit complex issue.
I have a program to read data from excel files using XSSFSheets. Everything works fine.
The problem i have is that for some users the program does not work if the files is located on dropbox. (Local version of dropbox, on their hard drive.)
The issue is not path related. Checked more times than i can count.
What is strange is that everyone has exactly the same admin rights for dropbox and office 365. The only difference i can find so far is that these users has windows 10.
If we place these excel files on the desktop, the program can find them.
Does anyone know if there is an issue for Java get excel sheets on dropbox for any reason? Can it have anything to do with windows 10 resp windows 8? If so why does it work on the windows 10 desktop.
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.JFrame;
import org.apache.poi.hssf.util.CellReference;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class xlmsReader {
public static ArrayList<XSSFSheet> fileReader() {
String directPath = System.getProperty("user.dir");
//removing the folder name, search through all folders in the parent folder, regardless of the location of the program folder.
directPath = directPath.substring(0, directPath.length()-18);
File[] directories = new File(directPath).listFiles(File::isDirectory);
ArrayList<XSSFSheet> excel = new ArrayList<XSSFSheet>();
for (int j = 0; j < directories.length; j++) {
if (new File(directories[j] + "\\Groning.xlsm").exists()) {
String str = directories[j] + "\\Groning.xlsm";
FileInputStream file;
file = null;
try {
file = new FileInputStream(new File(str));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
XSSFWorkbook workbook;
workbook = null;
try {
workbook = new XSSFWorkbook(file);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
XSSFSheet sheet = workbook.getSheet("Gröning");
sheet.getRow(0).getCell(0).setCellValue(directories[j].getPath());
excel.add(sheet);
}
}
return excel;
}
}
Hi everyone that answered.
I finally found out what was wrong.
The computors that run just Java JRE could not run the program.
Computors that ran Java JDK could. So it seems that i have used some function only available for developers. I dont know what exactly but issue solved, so I'm not going to put more effort to it.
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();
}
}
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();
}
}
}
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.
I used the following Java code for move the file form one directory to another, then execute the file in the destination directory. My code is
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import org.apache.commons.io.FileUtils;
public class JFileChooserTest {
public static void main(String[] args) {
String filelocation="C:\\Users\\FSSD\\Desktop\\OutPut\\Target";
File trgDir = new File(filelocation);
System.err
.println("file location>>>>>>>>>>>>>>>>>>>"
+ filelocation);
File desDir = new File(
"C:\\Users\\FSSD\\IndigoWorkSpace\\Swing\\test");
try {
FileUtils.copyDirectory(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("test\\setup.exe");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
While i executing this code, the file was successfully moved by i execute this code i got the following error.
java.io.IOException: Cannot run program "test\setup.exe": CreateProcess error=740, The requested operation requires elevation
at java.lang.ProcessBuilder.start(ProcessBuilder.java:460)
at java.lang.Runtime.exec(Runtime.java:593)
at java.lang.Runtime.exec(Runtime.java:431)
at java.lang.Runtime.exec(Runtime.java:328)
at JFileChooserTest.main(JFileChooserTest.java:34)
Caused by: java.io.IOException: CreateProcess error=740, The requested operation requires elevation
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.<init>(ProcessImpl.java:81)
at java.lang.ProcessImpl.start(ProcessImpl.java:30)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:453)
... 4 more
Here i used "setup.exe". It is an executable file. while i execute at run time i got the above error. Why it will happens, is any possible to resolve it. Thanks in advance..
I'm guessing you are using windows 7, when you run your batch process to start your JFileChooser test, right click and select "run as administrator".
The requested operation requires elevation
You do not have the rights to execute this setup. Probably UAC blocks it.
Use the runas command with Runtime#exec in Java.