Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
514 views
in Technique[技术] by (71.8m points)

excel - Producing a Java program for other systems

I've been working on a Java program for a company over the summer and it is nearing completion. Being something of a novice programmer, I've never produced a program for use on other systems using files I've created. My program reads and writes to Excel using Apache Poi, and currently the Excel file being used lies in a specific directory specified by the program. The program also uses some images that lie in directories specified by the code.

How could I make this program runnable on other systems? Would it be possible to have the program create an Excel document whenever it is "installed" on another system?

Currently I'm using Eclipse and the systems are windows 7.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can pack your application in a .jar, actually a zip file with inside .class files and resource files. Those resource files, like your images, can be taken with getClass().getResource("path/x.png") or getResourceAsStream. The path is either package relative or absolute: "/abc/def.jpg".

These resources must be read-only (as they are inside the .jar). However you may store an Excel template as resource, and copy that to the user's directory.

System.getProperty("user.home") 

Will give the user's directory where you may copy your resource to.

Path appWorkspace = Paths.get(System.getProperty("user.home"), "myapp");
Files.createDirectories(appWorkspace);
Path xlsx = appWorkspace.resolve("untitled.xlsx");
Files.copy(getClass().getResourceAsStream("/data/empty.xlsx"),
    xlsx);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...