How to catch all links that are in Windows clipboard? - java

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();
}
}
}

Related

Opening Page in IE using Java & Getting Confirmation on its load

I want to load a URL in the user's default browser. And once the webpage gets loaded completely I want to take its screenshot.
Currently I am doing it like this:
import java.awt.Desktop;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.net.URI;
import javax.imageio.ImageIO;
public class Example {
public static void main(String x[]) throws Exception
{
Desktop desktop = java.awt.Desktop.getDesktop();
URI oURL = new URI("http://192.168.1.125:8001/html/en/default/process/ProcessDesigner.jsp?wftId=81454277&wftVersion=1");
desktop.browse(oURL);
desktop.wait();
//desktop.print("");
Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
try {
Thread.sleep(20000); //1000 milliseconds is one second.
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
BufferedImage capture = new Robot().createScreenCapture(screenRect);
ImageIO.write(capture, "jpg", new File("E:\\Akram\\SSFolder\\fi2.jpg"));
System.out.println("Done");
}
}
Here I am using the sleep function so that I should be able to capture snapshot after screen gets loaded, but some web pages may take longer to load.
So I want confirmation that the web page is ready / loaded completely so that I can take its snapshot.
How can I achieve this?

Put specific image in folder from resource

I need put one specific image from my project to specific folder in Java. Thanks for helping.
Edit:
Im creating a folder with File and the folder I'm creating i need to put an image i have in resources. Thanks for helping.
You can use NIO package in Java 7 with the class Files and the static method copy.
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
public class Main_copie {
public static void main(String[] args) {
Path source = Paths.get("data/image1.png");
Path destination = Paths.get("MyFolder/image1_copied.png");
try {
Files.copy(source, destination, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Do you mean to copy the file from 1 location to another? If so, then the approach would be to create a new File at the desired location, create a FileOutputStream and write everything from the original File (using an FileInputStream) to this OutputStream.
Maybe this post can help you out.

package javax.media does not exist

I am working on an audio player and need to add pause() and play() features in it to connect with JButtons. The problem is I am not able to import Media package as it says package does not exist. I cannot find anywhere it online to download the package. Same goes for AudioPlayer class which gives bad class file error.
you need the JMF libraries , you can get them from there , for windows there is a typic installer :
JMF Download
Based on your question,
You can down load java.media
then use
import javax.media.*;
then you can declare like
Player audioplayer = Manager.createRealizedPlayer(file.toURI().toURL());
And
audioplayer.start(); and audioplayer.stop();
Here file means where the source file saved.
NB: you can use JMF jar file
Try like this
try {
audioplayer = Manager.createRealizedPlayer(file.toURI().toURL());
} catch (IOException ex) {
Logger.getLogger(MY_MP3_PLAYER.class.getName()).log(Level.SEVERE, null, ex);
} catch (NoPlayerException ex) {
Logger.getLogger(MY_MP3_PLAYER.class.getName()).log(Level.SEVERE, null, ex);
} catch (CannotRealizeException ex) {
Logger.getLogger(MY_MP3_PLAYER.class.getName()).log(Level.SEVERE, null, ex);
}
OR Try the sample code given below
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import javax.media.CannotRealizeException;
import javax.media.Manager;
import javax.media.NoPlayerException;
import javax.media.Player;
public class Mp3Player {
public static void main(String[] args) throws IOException, NoPlayerException, CannotRealizeException {
// Source of song file
File f=new File("your path in which mp3 file is saved");
// Create a Player object that realizes the audio
final Player p=Manager.createRealizedPlayer(f.toURI().toURL());
// Start the music
p.start();
// Create a Scanner object for taking input from cmd
Scanner s=new Scanner(System.in);
// Read a line and store it in st
String st=s.nextLine();
// If user types 's', stop the audio
if(st.equals("s"))
{
p.stop();
}
}
}
It is a late answer, but you can use the Maven dependency:
<!-- https://mvnrepository.com/artifact/javax.media/jmf -->
<dependency>
<groupId>javax.media</groupId>
<artifactId>jmf</artifactId>
<version>2.1.1e</version>
</dependency>
Following four packages will solve your problem. They contains most of helpful methods to deal with audio player.
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.AudioDevice;
import javazoom.jl.player.FactoryRegistry;
import javazoom.jl.player.advanced.AdvancedPlayer;
You can use .stop(), start(), .play() etc. from above packages.
Hope that will help.

Can not process the image on unix from opencv but same program works on windows

I have tested my program on windows platform, works perfectly. But when I try to run it on Raspbian, Unix, it doesn't go further after iplcvLoadimage.
What could be the problem?
Here is my code
mport com.googlecode.javacv.cpp.opencv_core.CvPoint;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
import static com.googlecode.javacv.cpp.opencv_core.cvSize;
import static com.googlecode.javacv.cpp.opencv_core.cvZero;
import static com.googlecode.javacv.cpp.opencv_core.cvMinMaxLoc;
import static com.googlecode.javacv.cpp.opencv_core.IPL_DEPTH_32F;
import static com.googlecode.javacv.cpp.opencv_core.cvCreateImage;
import static com.googlecode.javacv.cpp.opencv_imgproc.cvMatchTemplate;
import static com.googlecode.javacv.cpp.opencv_highgui.cvLoadImage;
import static com.googlecode.javacv.cpp.opencv_imgproc.CV_TM_SQDIFF;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import org.opencv.core.Core;
public class MatchTemplateTest {
public static HashMap<String,Double> getPatternMatch(String img)
{
ArrayList<String>names=new ArrayList<>();
HashMap<String,Double> MatchMap=new HashMap<>();
File []f=null;
try
{
String path=new File(new File(".").getCanonicalPath()+"/Output"+"/Logo").getAbsolutePath();
f=new File(path).listFiles();
} catch (Exception e) {
}
IplImage src=null;
try {
// this is where the problem is
src= cvLoadImage(img,0);
// can not process further
} catch (Exception e) {
System.out.println("ERROR "+e);
}
If the problem is with reading the image, then there are two possiblities
The image does not exist, or the path to the image is wrong. In *nix the path is separated with a slash '/', e.g. /home/asharma/data/myimage.pgm, but in windows it is separated with a backslash, e.g. C:\User\Data\My Image.pgm
The image decoder for the image does not exist in the version of the library. For example, if the image is in PNG format, but opencv was not compiled with PNG, then you cannot read the image.
what is the output of System.out.println("ERROR "+e);?

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