View Full Version : سوال: اضافه کردن فایل jar در هنگام اجرا برنامه
هانی هاشمی
شنبه 10 مرداد 1388, 22:18 عصر
سلام :افسرده:
یه برنامه نوشتم برای طراحی وب و می خوام مثل netbeans , eclipse قسمت plugin داشته باشه :عصبانی++: که متاسفانه به مشکلی برخوردم اونم اینه که نمی دونم چه جوری jar فایل های پلاگینم رو به برنامه اضافه کنم !!??
یعنی در هنگام اجرا آیا دستوری هست که بشه به library فایل jar اضافه کرد ؟؟؟
java.source.ir
یک شنبه 11 مرداد 1388, 11:35 صبح
سلام
فکر کنم جواب سؤالات شما را بدانم. اما اگر کمی بیشتر دربارهاش توضیح بدهی راحتتر بتوانم کمکت کنم.
هانی هاشمی
یک شنبه 11 مرداد 1388, 13:38 عصر
:چشمک:
دو تا مشکل هست
۱. می خوام موقع اجرا تمام فایل های jar یک فولدر به برنامم import بشن
۲. بعد از وارد شدن فایل های jar از کجا بفهمم چه فایل jarی و با چه اسمی import شده !
java.source.ir
دوشنبه 12 مرداد 1388, 11:31 صبح
به نام خداوند بخشنده بخشایشگر
سلام
هانی عزیز، چون گفته بودی برنامهات برای طراحی وب است، برای همین web application نوشتم. امیدوارم به دردت بخوره، اگر دیدی باز هم اونی نیست که میخوای بهم اطلاع بده تا باز هم نوعهای دیگرش را برات بفرستم. چون اگر بخوای میشه همین کار رو نیز برای desktop application به راحتی هرچه تمامتر انجام داد.
کاربرد این برنامه در جاهایی است که بخواهیم در صفحات وب خود به کاربران امکان upload کردن فایل بدهیم.
برنامه به این صورت است که صفحهای داریم و در آن کاربران قادر هستند فایلهای JAR به سرور اضافه نمایند. و بعد از آن مشخصات فایل اضافه شده به کاربران نمایش داده میشود. در ضمن همه فایلهای JAR نیز در یک آدرس مشخص از سرور ذخیره میشوند. تازه اگر هم بخواهی لیست همه فایلهای JAR برایت نشان داده شود، به راحتی و با اضافه کردن یک کد میتوانی اینکار را انجام دهی. اما فعلا در این برنامه اینکار را انجام ندادم، و با خود گفتم که اگر مورد پسند واقع گردید برایت کدهای مربوطه را بفرستم.
http://www.4shared.com/file/122548389/c59e316e/WebTest.html
موفق باشید.
هانی هاشمی
دوشنبه 12 مرداد 1388, 12:24 عصر
دستتون درد نکنه
متاسفانه من با jsp کار نکردم و نمی تونم اجراش کنم :ناراحت: بی زحمت نسخه دستاپش رو بدید :لبخند:
java.source.ir
دوشنبه 12 مرداد 1388, 17:15 عصر
دستتون درد نکنه
متاسفانه من با jsp کار نکردم و نمی تونم اجراش کنم :ناراحت: بی زحمت نسخه دستاپش رو بدید :لبخند:
باشه، هیچ ایرادی نداره. به امید خدا در اسرع وقت یک نمونه پروژه Desktop Application برات تو سایت میذارم.
با نهایت احترام
java.source.ir
سه شنبه 13 مرداد 1388, 10:23 صبح
سلام، این هم یک نمونه برنامه Desktop Application
کلاس اصلی برنامه:
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DesktopTest extends JFrame implements ActionListener
{
private JLabel lbl1, lbl2;
private JButton search;
private JTextArea textArea;
private Container container;
private BorderLayout bLayout;
public DesktopTest() {
super("DesktopTest.java");
bLayout = new BorderLayout();
container = getContentPane();
container.setLayout(bLayout);
JPanel n, m, s;
n = new JPanel();
m = new JPanel();
s = new JPanel();
lbl1 = new JLabel
("لطفا برای اضافه کردن فایل JAR کلید روبهرو را کلیک نمایید:");
search = new JButton("جستجو");
search.addActionListener(this);
lbl2 = new JLabel("لیست فایلهای اضافه شده:");
textArea = new JTextArea("");
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
textArea.setEditable(false);
textArea.setColumns(80);
textArea.append("Source Location" + "\t\t" + "Name" + "\t\t"
+ "Destination Location");
textArea.append("\n");
n.add(search);
n.add(lbl1);
m.add(lbl2);
s.add(textArea);
container.add(n, BorderLayout.NORTH);
container.add(m, BorderLayout.CENTER);
container.add(s, BorderLayout.SOUTH);
pack();
setVisible(true);
setLocationRelativeTo(null);
}
public void actionPerformed(ActionEvent event) {
String fileDir = null, fileName=null;
JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.FILES_AND_DIR ECTORIES);
jarFilter jar = new jarFilter();
fc.setFileFilter(jar);
int selection = fc.showOpenDialog(null);
File file = fc.getSelectedFile();
if (selection == JFileChooser.APPROVE_OPTION) {
fileDir = file.toString();
int len = fileDir.lastIndexOf("\\");
fileName = fileDir.substring(len + 1);
int len2 = fileName.lastIndexOf(".");
String fileType = fileName.substring(len2 + 1);
if(fileType.equals("jar")) {
copyfile(fileDir, "lib\\" + fileName);
textArea.append(fileDir + "\t\t" + fileName + "\t\t"
+ "lib\\" + fileName);
textArea.append("\n");
pack();
}
}
}
private static void copyfile(String srFile, String dtFile){
try{
File f1 = new File(srFile);
File f2 = new File(dtFile);
InputStream in = new FileInputStream(f1);
OutputStream out = new FileOutputStream(f2);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0){
out.write(buf, 0, len);
}
in.close();
out.close();
System.out.println("File copied.");
}
catch(FileNotFoundException ex){
System.out.println(ex.getMessage() + " in the specified directory.");
System.exit(0);
}
catch(IOException e){
System.out.println(e.getMessage());
}
}
public static void main(String[] args) {
DesktopTest app = new DesktopTest();
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
}
}
** فقط به این نکته توجه داشته باش که باید در مسیر پروژه خود از قبل یک فولدر با نام lib داشته باشی.
کلاسهای جانبی:
import java.io.File;
import javax.swing.*;
import javax.swing.filechooser.*;
public class jarFilter extends FileFilter {
public boolean accept(File f) {
if (f.isDirectory()) {
return true;
}
String extension = Utils.getExtension(f);
if (extension != null) {
if (extension.equals(Utils.jar)) {
return true;
} else {
return false;
}
}
return false;
}
//The description of this filter
public String getDescription() {
return "Just JAR Files, Please";
}
}
import java.io.File;
import javax.swing.ImageIcon;
public class Utils {
public final static String jar = "jar";
/*
* Get the extension of a file.
*/
public static String getExtension(File f) {
String ext = null;
String s = f.getName();
int i = s.lastIndexOf('.');
if (i > 0 && i < s.length() - 1) {
ext = s.substring(i+1).toLowerCase();
}
return ext;
}
}
امیدوارم به دردت بخوره.
** بازهم بهتر از این می شود این برنامه را در آورد، ولی ترجیحا یک مدل پایه از آن را پیاده سازی نمودم.
موفق و سربلند باشید.
Syamah
سه شنبه 13 مرداد 1388, 11:03 صبح
با استفاده از کلاس URLClassLoader می توانید یک برنامه ی Plug-in Based Programming ایجاد نمایید.
java.source.ir
سه شنبه 13 مرداد 1388, 11:21 صبح
با استفاده از کلاس URLClassLoader می توانید یک برنامه ی Plug-in Based Programming ایجاد نمایید.
سلام
لطفا اگر مقدور است یک مثال کاربردی برام قرار بده، تا با شیوه کار مطلب اشاره شده توسط شما بیشتر آشنا شوم.
البته در این رابطه نیز جستجویی انجام دادم و مثال زیر را پیدا کردم: (هم برای فایلهای JAR در local و هم برای remote کاربرد دارد)
** برای استفاده از local باید از آدرس URL هایی مثل زیر استفاده نمود:
file:///C:/Program%20Files/Java/jdk1.6.0_02/bin/a.jar
import java.net.URL;
import java.net.URLClassLoader;
import java.net.JarURLConnection;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.InvocationTargetException;
import java.util.jar.Attributes;
import java.io.IOException;
/**
* A class loader for loading jar files, both local and remote.
*/
class JarClassLoader extends URLClassLoader {
private URL url;
/**
* Creates a new JarClassLoader for the specified url.
*
* @param url the url of the jar file
*/
public JarClassLoader(URL url) {
super(new URL[] { url });
this.url = url;
}
/**
* Returns the name of the jar file main class, or null if
* no "Main-Class" manifest attributes was defined.
*/
public String getMainClassName() throws IOException {
URL u = new URL("jar", "", url + "!/");
JarURLConnection uc = (JarURLConnection)u.openConnection();
Attributes attr = uc.getMainAttributes();
return attr != null ? attr.getValue(Attributes.Name.MAIN_CLASS) : null;
}
/**
* Invokes the application in this jar file given the name of the
* main class and an array of arguments. The class must define a
* static method "main" which takes an array of String arguemtns
* and is of return type "void".
*
* @param name the name of the main class
* @param args the arguments for the application
* @exception ClassNotFoundException if the specified class could not
* be found
* @exception NoSuchMethodException if the specified class does not
* contain a "main" method
* @exception InvocationTargetException if the application raised an
* exception
*/
public void invokeClass(String name, String[] args)
throws ClassNotFoundException,
NoSuchMethodException,
InvocationTargetException
{
Class c = loadClass(name);
Method m = c.getMethod("main", new Class[] { args.getClass() });
m.setAccessible(true);
int mods = m.getModifiers();
if (m.getReturnType() != void.class || !Modifier.isStatic(mods) ||
!Modifier.isPublic(mods)) {
throw new NoSuchMethodException("main");
}
try {
m.invoke(null, new Object[] { args });
} catch (IllegalAccessException e) {
// This should not happen, as we have disabled access checks
}
}
}
import java.io.IOException;
import java.net.URL;
import java.net.MalformedURLException;
import java.lang.reflect.InvocationTargetException;
/**
* Runs a jar application from any url. Usage is 'java JarRunner url [args..]'
* where url is the url of the jar file and args is optional arguments to
* be passed to the application's main method.
*/
public class JarRunner {
public static void main(String[] args) {
if (args.length < 1) {
usage();
}
URL url = null;
try {
url = new URL(args[0]);
} catch (MalformedURLException e) {
fatal("Invalid URL: " + args[0]);
}
// Create the class loader for the application jar file
JarClassLoader cl = new JarClassLoader(url);
// Get the application's main class name
String name = null;
try {
name = cl.getMainClassName();
} catch (IOException e) {
System.err.println("I/O error while loading JAR file:");
e.printStackTrace();
System.exit(1);
}
if (name == null) {
fatal("Specified jar file does not contain a 'Main-Class'" +
" manifest attribute");
}
// Get arguments for the application
String[] newArgs = new String[args.length - 1];
System.arraycopy(args, 1, newArgs, 0, newArgs.length);
// Invoke application's main class
try {
cl.invokeClass(name, newArgs);
} catch (ClassNotFoundException e) {
fatal("Class not found: " + name);
} catch (NoSuchMethodException e) {
fatal("Class does not define a 'main' method: " + name);
} catch (InvocationTargetException e) {
e.getTargetException().printStackTrace();
System.exit(1);
}
}
private static void fatal(String s) {
System.err.println(s);
System.exit(1);
}
private static void usage() {
fatal("Usage: java JarRunner url [args..]");
}
}
Syamah
پنج شنبه 15 مرداد 1388, 11:33 صبح
ابتدا اینترفیس زیر را برنامه اضافه می کنیم
package Plugin;
public interface IPlugin {
String getName();
String getAuthor();
void Run(Object... par);
}
سپس کد زیر را می نویسیم :
package host;
import Plugin.IPlugin;
import java.net.URL;
import java.net.URLClassLoader;
public class Main {
public static void main(String[] args) {
try {
URL url = new URL("file:///c:/TestPlugin.jar");
URLClassLoader pluginLoader = new URLClassLoader(new URL[]{url});
Class<?> cl = pluginLoader.loadClass("test.example");
IPlugin plg = (IPlugin) cl.newInstance();
plg.Run();
} catch (Exception exp) { }
}
}
این هم یک نمونه پلاگین :
package test;
import Plugin.IPlugin;
public class example implements IPlugin{
public String getName() {
return "Example Plug-in";
}
public String getAuthor() {
return "Halo";
}
public void Run(Object... par) {
System.out.print("OK!\n");;
}
}
Syamah
شنبه 17 مرداد 1388, 09:16 صبح
یک نمونه که با net beans 6.5 توسط خودم نوشته شده است.
vBulletin® v4.2.5, Copyright ©2000-1404, Jelsoft Enterprises Ltd.