java - জার ফাইল বাইরে সম্পত্তি ফাইল পড়ুন
properties (4)
আমি একটি জার ফাইল আছে যেখানে আমার সব কোড চলমান জন্য সংরক্ষণ করা হয়। আমি একটি বৈশিষ্ট্য ফাইল অ্যাক্সেস করতে হবে যা প্রতিটি রান করার আগে পরিবর্তন / সম্পাদনা করতে হবে। আমি JAR ফাইল যেখানে একই ডিরেক্টরির মধ্যে বৈশিষ্ট্য ফাইল রাখতে চান। যে ডিরেক্টরী থেকে বৈশিষ্ট্য ফাইল নিতে জাভা বলতে যাই হোক না কেন?
নোট: আমি হোম ডিরেক্টরিতে বৈশিষ্ট্য ফাইল রাখতে চাই না বা কমান্ড লাইন আর্গুমেন্টে বৈশিষ্ট্য ফাইলের পথ পাস করতে চাই না।
আমারও অনুরূপ কেস আছে: *.jar
ফাইলটি *.jar
ফাইলের পাশে একটি ডিরেক্টরির মধ্যে একটি ফাইল অ্যাক্সেস করতে চাই। পাশাপাশি এই উত্তর পড়ুন।
আমার ফাইল গঠন:
./ - the root of your program
|__ *.jar
|__ dir-next-to-jar/some.txt
আমি *.jar
ফাইলের ভিতরে একটি ইনপুট some.txt
একটি ফাইল (বলে, some.txt
) লোড করতে সক্ষম হচ্ছি:
InputStream stream = null;
try{
stream = ThisClassName.class.getClass().getResourceAsStream("/dir-next-to-jar/some.txt");
}
catch(Exception e) {
System.out.print("error file to stream: ");
System.out.println(e.getMessage());
}
তারপর আপনি stream
সঙ্গে যাই হোক না কেন
আমি log4j2.properties সঙ্গে ক্লাসপথ বা বহিরাগত কনফিগারেশন উভয় করছেন একটি উদাহরণ আছে
package org.mmartin.app1;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.LogManager;
public class App1 {
private static Logger logger=null;
private static final String LOG_PROPERTIES_FILE = "config/log4j2.properties";
private static final String CONFIG_PROPERTIES_FILE = "config/config.properties";
private Properties properties= new Properties();
public App1() {
System.out.println("--Logger intialized with classpath properties file--");
intializeLogger1();
testLogging();
System.out.println("--Logger intialized with external file--");
intializeLogger2();
testLogging();
}
public void readProperties() {
InputStream input = null;
try {
input = new FileInputStream(CONFIG_PROPERTIES_FILE);
this.properties.load(input);
} catch (IOException e) {
logger.error("Unable to read the config.properties file.",e);
System.exit(1);
}
}
public void printProperties() {
this.properties.list(System.out);
}
public void testLogging() {
logger.debug("This is a debug message");
logger.info("This is an info message");
logger.warn("This is a warn message");
logger.error("This is an error message");
logger.fatal("This is a fatal message");
logger.info("Logger's name: "+logger.getName());
}
private void intializeLogger1() {
logger = LogManager.getLogger(App1.class);
}
private void intializeLogger2() {
LoggerContext context = (org.apache.logging.log4j.core.LoggerContext) LogManager.getContext(false);
File file = new File(LOG_PROPERTIES_FILE);
// this will force a reconfiguration
context.setConfigLocation(file.toURI());
logger = context.getLogger(App1.class.getName());
}
public static void main(String[] args) {
App1 app1 = new App1();
app1.readProperties();
app1.printProperties();
}
}
--Logger intialized with classpath properties file--
[DEBUG] 2018-08-27 10:35:14.510 [main] App1 - This is a debug message
[INFO ] 2018-08-27 10:35:14.513 [main] App1 - This is an info message
[WARN ] 2018-08-27 10:35:14.513 [main] App1 - This is a warn message
[ERROR] 2018-08-27 10:35:14.513 [main] App1 - This is an error message
[FATAL] 2018-08-27 10:35:14.513 [main] App1 - This is a fatal message
[INFO ] 2018-08-27 10:35:14.514 [main] App1 - Logger's name: org.mmartin.app1.App1
--Logger intialized with external file--
[DEBUG] 2018-08-27 10:35:14.524 [main] App1 - This is a debug message
[INFO ] 2018-08-27 10:35:14.525 [main] App1 - This is an info message
[WARN ] 2018-08-27 10:35:14.525 [main] App1 - This is a warn message
[ERROR] 2018-08-27 10:35:14.525 [main] App1 - This is an error message
[FATAL] 2018-08-27 10:35:14.525 [main] App1 - This is a fatal message
[INFO ] 2018-08-27 10:35:14.525 [main] App1 - Logger's name: org.mmartin.app1.App1
-- listing properties --
dbpassword=password
database=localhost
dbuser=user
একটি জার ফাইল থেকে আপনার ফাইল ডিরেক্টরি ফাইল অ্যাক্সেস সবসময় একটি সমস্যা আছে। একটি জার ফাইল মধ্যে ক্লাসপথ প্রদান খুব সীমিত। পরিবর্তে আপনার প্রোগ্রাম শুরু করার জন্য একটি ব্যাট ফাইল বা একটি sh ফাইল ব্যবহার করে দেখুন। এই ভাবে আপনি আপনার ক্লাসপথটি যেভাবেই পছন্দ করেন তা সিস্টেমের যেকোনো ফোল্ডারে উল্লেখ করতে পারেন।
এছাড়াও এই প্রশ্নের উপর আমার উত্তর চেক করুন:
এটা আমার জন্য কাজ করে। current directory
থেকে আপনার বৈশিষ্ট্য ফাইল লোড করুন
Properties properties = new Properties();
properties.load(new FileReader(new File(".").getCanonicalPath() + File.separator + "java.properties"));
properties.forEach((k, v) -> {
System.out.println(k + " : " + v);
});
নিশ্চিত করুন, java.properties
current directory
। আপনি শুধু একটি ছোট স্টার্টআপ স্ক্রিপ্ট লিখতে পারেন যা আগের মতো ডান ডিরেক্টরিতে স্যুইচ করে
#! /bin/bash
scriptdir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd $scriptdir
java -jar MyExecutable.jar
cd -
আপনার প্রোজেক্টে আপনার java.properties
ফাইলটি ঠিক করুন, যাতে আপনার কোডটি আপনার আইডিই থেকেও কাজ করতে পারে।