Error while migrating JSP code from Windows to Linux machine - java

I worked on a JSP code that is runnning on a Tomcat5.5 server in windows system .
I had to copy all the JSP code to a linux system and when I did the same I got an error stating below.
javax.servlet.ServletException: c:\tmp is not a directory
Readcsv.init(Readcsv.java:36)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:174)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:875)
org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
java.lang.Thread.run(Thread.java:636)
I modified a java code in the windows system without that c:\tmp directory and restarted the tomcat server and the tool worked fine.
When I replaced the modified java code of windows to the linux system, I still get the same error.
Note: Am accessing the linux server from windows using the url http://192.168.0.85:8080/CNA/uploadcsv.jspwhere 85 is the system number of linux.
Is there anything like tomcat has to be restarted for the linux version too? If so how to do the same?
UPDATE
This is where I have used the c:\tmp location in my code.
public class Readcsv extends HttpServlet {
private static final String TMP_DIR_PATH = "c:\tmp";
private File tmpDir;
private static final String DESTINATION_DIR_PATH ="/files";
private File destinationDir;
public void init(ServletConfig config) throws ServletException {
super.init(config);
tmpDir = new File(TMP_DIR_PATH);
if(!tmpDir.isDirectory()) {
throw new ServletException(TMP_DIR_PATH + " is not a directory");
}
String realPath = getServletContext().getRealPath(DESTINATION_DIR_PATH);
destinationDir = new File(realPath);
if(!destinationDir.isDirectory()) {
throw new ServletException(DESTINATION_DIR_PATH+" is not a directory");
}
}
How can I find the replacement for the temp path? The destination path works fine.
I exactly copied the code from this example

Don't hardcode disk file system paths in your code. That's only portability and maintainability trouble.
In case of temporary files, rather make use of File#createTempFile().
File tempfile = File.createTempFile("name", ".ext");
It will automatically create the temp file at the right location, regardless of the environment. You can however also obtain the tmp dir root location by System.getProperty("java.io.tmpdir");.
In case of resources which are to be read by your application, just put them in the runtime classpath or add their path to the runtime classpath. Then you can just obtain them from the classpath by getResource() and getResourceAsStream() methods on Class or ClassLoader.
InputStream input = getClass().getResourceAsStream("file.properties");
If you really need to have a fixed path outside the classpath, then rather define it in a properties file so that you at least have any control over the path from outside the application (so, without the need to change the code everytime).
String path = properties.getProperty("my.file.path");

it seems that your application try to read a csv file under "C:\tmp" which doesn't exist on your linux system.

You said you modified the code and redeployed it to Tomcat.
You probably just need to restart Tomcat to get it to pick up the new code. Until then, it will be running the old code and you will get the same error.
How you restart Tomcat depends on which Linux distribution you are running and how you installed Tomcat.

Related

Unable to load library (libFile.so) on jboss server on redhat linux

I am getting the unsatisfied link error when I try to run the web-app.
Suppressed: java.lang.UnsatisfiedLinkError: libXXXX.so: cannot open shared object file: No such file or directory
at deployment.ttt.war//com.sun.jna.Native.open(Native Method)
at deployment.ttt.war//com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:191)
... 74 more
Suppressed: java.lang.UnsatisfiedLinkError: libXXXX.so: cannot open shared object file: No such file or directory
at deployment.ttt.war//com.sun.jna.Native.open(Native Method)
at deployment.ttt.war//com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:204)
... 74 more
Suppressed: java.io.IOException: Native library (linux-x86-64/libXXXX.so) not found in resource path (/opt/jboss-eap-7.3/jboss-modules.jar)
at deployment.ttt.war//com.sun.jna.Native.extractFromResourcePath(Native.java:1095)
at deployment.ttt.war//com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:275)
... 74 more
I have created the function which loads the native library stored at "/home/libraryFiles", using JNA.
I have stored all of my libXXXX.so files at "/home/libraryFiles".
I have exporting my war file from eclipse in windows and deploying it on the jboss server on redhat linux.
This is my function :
public class function1(){
public interface CLibrary extends Library {
public int method1(String message);
}
public int execute (String param) throws Exception{
NativeLibrary.addSearchPath("libXXXX", "home/libraryFiles");
CLibrary pLib =(CLibrary)Native.loadLibrary("XXXX",CLibrary.class);
return pLib.method1(param);
}
}
I am mapping url through rest controller to execute
new function1().execute("aaaaaaa");
I have also tried setting jna.library.path & java.library.path to "home/libraryFiles", but of no use. (using system.setProperty())
I also tried set $LD_LIBRARY_PATH=home/libraryFiles but still no good.
Seems like my web-app is not able to point out of the default resource path "/opt/jboss-eap-7.3/jboss-modules.jar"
Any help is welcoming.
PS :
I tried the same function/code on my windows PC, its working fine. I don't know why its not working on redhat linux.
Thanks in Advance.
The addSearchPath() method is specific to a library, and the additional path(s) are stored in a map with the library name as the key.
The loadLibrary() method checks that map using the library name.
You have used differing strings as the key to store the path and retrieve it:
NativeLibrary.addSearchPath("libXXXX", "home/libraryFiles");
CLibrary pLib =(CLibrary)Native.loadLibrary("XXXX",CLibrary.class);
You should either change "libXXXX" to "XXXX" in the first line (probably the preferred style) or do the reverse in the second line to match.
As noted in the comments, you also must be careful with relative vs. absolute file paths and directory permissions.

How to specify a Java file path for both Mac and Ubuntu

I'm using IntelliJ and Spring and Java to locally develop an app on a Mac, and then deploy to a tomcat server on AWS, using Ubuntu 16.04.3 LTS (GNU/Linux 4.4.0-1048-aws x86_64).
I'm having trouble specifying the file path so that it works in both environments.
My code is
InputStream fileStream = new FileInputStream("src/main/resources/static/web/data/ReportDates.json");
JsonReader reader = Json.createReader(fileStream);
JsonObject reportDates = reader.readObject();
reader.close();
When I run locally, the file is read in correctly. It is located in:
src/main/resources/static/web/data/ReportDates.json
But when I deploy, that code results in the error message:
java.io.FileNotFoundException: src/main/resources/static/web/data/ReportDates.json (No such file or directory)
The actual location of the file on that machine turns out to be:
/opt/tomcat/webapps/automentor/WEB-INF/classes/static/web/data/ReportDates.json
How can I specify the file path so that it works correctly in both environments?
I have given up on using a single path. #Nicholas Pesa got me thinking -- since I use IDEA, I don't have a fixed WEB-INF folder, so it's easier for me to change the path that should be used than to move the file to a fixed location.
My code now uses:
String filepath = (new File("src/main/resources/static/web/data/ReportDates.json").exists()) ? "src/main/resources/static/web/data/ReportDates.json" : "/opt/tomcat/webapps/automentor/WEB-INF/classes/static/web/data/ReportDates.json";

How to pass variable file path to SQL*Loader on Linux

I'm creating .csv file and placing it in one location.
String location = "/APPL_TOP2/IMDD/apps/apps_st/appl/xxfin/12.0.0/bin/xe.csv";
Using exact path working fine on Windows but I need it to work on Linux client too.
String location = "$XXFIN_TOP\\12.0.0\\bin\\xe.csv";
If I'm using relevant path on Linux it is not working, showing Error
SQL*Loader-500: Unable to open file(/APPL_TOP2/IMDD/apps/apps_st/appl/xxfin/12.0.0/bin/xe.csv)
SQL*Loader-553: file not found
SQL*Loader-509: System error: No such file or directory
" Client is asking any option to pass relevant path client machine is Linux"
Paths in Linux have slashes which go the other way. So it should be this:
$XXFIN_TOP/12.0.0/bin/xe.csv

JNativeHook_6363198016012433909.dll: Access is denied

Exception in thread "AWT-EventQueue-0" java.lang.UnsatisfiedLinkError: C:\Users\Kelvz1\AppData\Local\Temp\JNativeHook_6363198016012433909.dll: Access is denied
I have these error how can I fix it.
Some of the users can access these and no problem, but some user cant.
This is a strange random error that sometimes manifests on Windows 7 and Windows 8. Everything is fine one day and then suddenly you get an access denied exception when Java tries to access a DLL in the temp folder.
I've found deleting the TEMP folder and letting it re-create it automatically usually solves the problem.
If you are the author of the code that put the DLL in the TEMP folder then I would recommend you change the code to put the DLL in a folder under this path instead as I have yet to see this problem there: %USERPROFILE%\AppData\Local\
I read somewhere this can be caused by Microsoft Security Essentials but it doesn't look like this is installed on the computer that just experienced this problem.
I've seen this happen with many different DLL files, like jna.dll.
If you're using JNA and it has this problem you can change the temp directory system property and JNA will create the file in a different directory. This code should work for this.
String osName = System.getProperty("os.name");
if (osName.toLowerCase().startsWith("windows")) {
// we change the temp directory because sometimes Windows is stupid and doesn't want to load jna.dll from the temp directory
File tempDir = new File(System.getenv("USERPROFILE") + "\\AppData\\Local\\MyCompany\\temp");
System.out.println("Using temp dir: " + tempDir.getPath());
tempDir.mkdirs();
System.setProperty("java.io.tmpdir", tempDir.getPath());
}

Deployment in tomcat

i am getting a problem
i have deployed a war file, when i run localy through tomcat it works fine but when i run on another system by giveing my system ip and then project folder e.g
http:\192.168.0.145\DllTest it loads the applet but when i click on a button to load the functionality it is throwing an exception
Exception in thread "AWT-EventQueue-3" java.lang.UnsatisfiedLinkError: Expecting an absolute path of the library: http:\192.168.0.145:8080\DllTest\lib\jinvoke.dll
while it is working fine localy but not in another system. Please tell me what is the problem.
Is it a rights issue or something else.
You cannot load a DLL on an external host. It has to be an absolute disk file system -as the exception message already hints. Your best bet is to download it manually, create a temp file and load it instead.
File dllFile = File.createTempFile("jinvoke", ".dll");
InputStream input = new URL(getCodeBase(), "lib/jinvoke.dll").openStream();
OuptutStream output = new FileOutputStream(dllFile);
// Write input to output and close streams the usual Java IO way.
// Then load it using absolute disk file system path.
System.loadLibrary(dllFile.getAbsolutePath());
dllFile.deleteOnExit();

Categories