使用JAVA创建Windows下的快捷方式

方法一:使用JS

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;


public class MakeShortCut {
public static String osName = System.getProperty("os.name");
/**
* @param args
*/
public static void main(String[] args) {


MakeShortCut sc = new MakeShortCut();
File installDir = new File("D:\\test\\");
sc.createWindowsShortcut(installDir, "SQLite Database Browser.exe",
"test", "方正智汇");
}
/**
*
* @param installDir
* 文件路径
* @param runnable
* 可执行性文件(***.exe)
* @param folder
* 文件夹名称(本级文件夹名称)
* @param name
* 快捷方式名称
* @return
*/
public boolean createWindowsShortcut(File installDir, String runnable,
String folder, String name)
{
String command = null;
if (osName.indexOf("9") != -1)
command = "command.com /c cscript.exe /nologo ";
else
command = "cmd.exe /c cscript.exe /nologo ";
if (command != null)
try
{
File shortcutMaker = new File(installDir, "makeshortcut.js");
PrintStream out = new PrintStream(new FileOutputStream(
shortcutMaker));
out.println("WScript.Echo(\"Creating shortcuts...\");");
out
.println("Shell = new ActiveXObject(\"WScript.Shell\");");
out
.println("ProgramsPath = Shell.SpecialFolders(\"Programs\");");
/** 创建菜单快捷方式 */
out
.println("fso = new ActiveXObject(\"Scripting.FileSystemObject\");");
out.println("if (!fso.folderExists(ProgramsPath + \"\\\\"
+ folder + "\"))");
out.println("\tfso.CreateFolder(ProgramsPath + \"\\\\"
+ folder + "\");");
out
.println("link = Shell.CreateShortcut(ProgramsPath + \"\\\\"
+ folder + "\\\\" + name + ".lnk\");");
out.println("link.Arguments = \"\";");
out.println("link.Description = \"" + name + "\";");
out.println("link.HotKey = \"\";");
out.println("link.IconLocation = \""
+ escaped(installDir.getAbsolutePath()) + "\\\\"
+ "winamp.ico,0\";");
out.println("link.TargetPath = \""
+ escaped(installDir.getAbsolutePath()) + "\\\\"
+ runnable + "\";");
out.println("link.WindowStyle = 1;");
out.println("link.WorkingDirectory = \""
+ escaped(installDir.getAbsolutePath()) + "\";");
out.println("link.Save();");
/** 创建桌面快捷方式 */
out
.println("DesktopPath = Shell.SpecialFolders(\"Desktop\");");
out
.println("link = Shell.CreateShortcut(DesktopPath + \"\\\\"
+ name + ".lnk\");");
out.println("link.Arguments = \"\";");
out.println("link.Description = \"" + name + "\";");
out.println("link.HotKey = \"\";");
out.println("link.IconLocation = \""
+ escaped(installDir.getAbsolutePath()) + "\\\\"
+ "winamp.ico,0\";");
out.println("link.TargetPath = \""
+ escaped(installDir.getAbsolutePath()) + "\\\\"
+ runnable + "\";");
out.println("link.WindowStyle = 1;");
out.println("link.WorkingDirectory = \""
+ escaped(installDir.getAbsolutePath()) + "\";");
out.println("link.Save();");
out.println("WScript.Echo(\"Shortcuts created.\");");
/** ********* */
out.close();
//Process p = Runtime.getRuntime().exec( command + " makeshortcut.js", null, installDir);
Process p = Runtime.getRuntime().exec( "C:\\Documents and Settings\\Walker Zhang\\桌面\\PCS_Crawler_exe\\pcs_crawler.exe");
p.waitFor();
int rv = p.exitValue();
if (rv == 0)
{
// JOptionPane.showMessageDialog(null, "创建成功");
return true;
} else
{
return false;
}
} catch (Exception e)
{
return false;
}
else
return false;
}

public String escaped(String s)
{
String r = "";
for (int i = 0; i < s.length(); i++)
{
if (s.charAt(i) == '\\')
r = r + '\\';
r = r + s.charAt(i);
}

return r;
}
}


方法二:使用
JShortcut,需下载库

[url]http://alumnus.caltech.edu/~jimmc/jshortcut/[/url]

package com.test;

import net.jimmc.jshortcut.JShellLink;

public class TestS {
public static void main(String[] args) {
String jarFileName = "D:\\mhtest";
// create lnk file
JShellLink link = new JShellLink();
link.setFolder("C:\\");
link.setName("mhtest");
link.setPath(jarFileName);
link.save();

// get the real file the lnk file link to.
JShellLink link2 = new JShellLink("C:\\", "mhtest");
link2.load();
String sPath = link2.getPath();
System.out.print(sPath);
int i = 0;
i++;
}
}


转载自http://nakeyfishzh.blog.163.com/blog/static/9826442120101595031153/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值