How to move file inside Program Files using java? - java

I need to programmatically move some conf file inside Program Files. Target environment is windows. Here is the example code I have:
https://gist.github.com/gandra/911f465217011e2dfec1a8e561c0bb57
but after execution I receive following error:
"C:\Program Files\Java\jdk1.8.0_221\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2019.2\lib\idea_rt.jar=59066:C:\Program Files\JetBrains\IntelliJ IDEA 2019.2\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_221\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\rt.jar;C:\work\kib-smartstart\src\postdeploy-validation-sahipro\target\classes;C:\work\kib-smartstart\src\postdeploy-validation-sahipro\local-maven-repo\sahi.7.5.0\sahi.jar;C:\work\kib-smartstart\src\postdeploy-validation-sahipro\local-maven-repo\sahi.7.5.0\ant-sahi.jar;C:\Users\draganmijatovic\.m2\repository\org\testng\testng\6.14.3\testng-6.14.3.jar;C:\Users\draganmijatovic\.m2\repository\com\beust\jcommander\1.72\jcommander-1.72.jar;C:\Users\draganmijatovic\.m2\repository\org\apache-extras\beanshell\bsh\2.0b6\bsh-2.0b6.jar;C:\Users\draganmijatovic\.m2\repository\org\apache\logging\log4j\log4j-core\2.12.0\log4j-core-2.12.0.jar;C:\Users\draganmijatovic\.m2\repository\org\apache\logging\log4j\log4j-api\2.12.0\log4j-api-2.12.0.jar;C:\Users\draganmijatovic\.m2\repository\commons-io\commons-io\2.6\commons-io-2.6.jar" MoveFilesDemo
Exception in thread "main" java.nio.file.AccessDeniedException: C:\temp\testfile.txt -> C:\Program Files\testfile.txt
at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:83)
at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
at sun.nio.fs.WindowsFileCopy.move(WindowsFileCopy.java:387)
at sun.nio.fs.WindowsFileSystemProvider.move(WindowsFileSystemProvider.java:287)
at java.nio.file.Files.move(Files.java:1395)
at MoveFilesDemo.main(MoveFilesDemo.java:10)
Process finished with exit code 1
Is there a way to run it as admin or any other option to achieve it? To setup some java permission etc ... Please do not advice me to keep this file in Program Data or repeating that I should not bypass this like in following question: Bypass Windows permission restrictions on program files folder
This is part of some automated testing of some legacy desktop app and need fix to this problem in order to test it. So it is totally acceptable for me to bypass security or any other option which will help me to achieve this without human intervention during execution is welcome.

You need to change the permission like that :
// changing the file permissions
file.setExecutable(true);
file.setReadable(true);
file.setWritable(true);

Related

Cannot open sever.jar file

So I was trying to make a 1.17.1 minecraft server on my mac. I couldn't open my 1.17.1_server.jar with Java 8 so I download Java 16.0.2.
Unfortunately, everytime I was opening the 1.17.1_server.jar file, I got
"The Java JAR file "1.17._server.jar" could not be launched." .
I first thought that it was because the file was launch by Java 8 instead of 16.
So I went into the terminal and run :<path to java> -jar 1.17.1_server.jar
I then got this : Error: Unable to access jarfile 1.17.1_server.jar
Finally i tried to put the path of the jar file in the command...
So I've run : path to java -jar path to server
and got this :
[main/ERROR]: Failed to load properties from file: server.properties
[15:57:35] [main/WARN]: Failed to load eula.txt
[15:57:35] [main/INFO]: You need to agree to the EULA in order to run the server. Go to eula.txt for more info.
So why I have to agreed Eula if i've never launched it ? Does it think that he already been launched ?
As stated in the error message
[15:57:35] [main/INFO]: You need to agree to the EULA in order to run the server. Go to eula.txt for more info.
You have to open the file and check yes
Okay find the solution: my eula and server properties file were in my user folder dk why (never moved them so...)

Wix pre-processor rejecting class files with `$anon$` in them from a Scala application

I'm trying to automate the build process of my Scala application for producing a msi installer.
When doing so, windows claims that certain files cannot be found. Here is the command that gets run
[21:39:01.842] Running C:\Program Files (x86)\WiX Toolset v3.11\bin\candle.exe in C:\Users\RUNNER~1\AppData\Local\Temp\jdk.jpackage6105194214114584024\images\win-msi.image\krystalbull
and further down, i start seeing errors like this:
C:\Users\runneradmin\AppData\Local\Temp\jdk.jpackage6105194214114584024\config\bundle.wxf(56) : error LGHT0103 : The system cannot find the file 'C:\Users\RUNNER~1\AppData\Local\Temp\jdk.jpackage6105194214114584024\images\win-msi.image\krystalbull\app\classes\com\krystal\bull\gui\FileMenu$anon$2.class'.
C:\Users\runneradmin\AppData\Local\Temp\jdk.jpackage6105194214114584024\config\bundle.wxf(71) : error LGHT0103 : The system cannot find the file 'C:\Users\RUNNER~1\AppData\Local\Temp\jdk.jpackage6105194214114584024\images\win-msi.image\krystalbull\app\classes\com\krystal\bull\gui\dialog\ViewEventDialog$anon$10$anon$21$anonfun$lessinit$greater$1.class'.
C:\Users\runneradmin\AppData\Local\Temp\jdk.jpackage6105194214114584024\config\bundle.wxf(81) : error LGHT0103 : The system cannot find the file 'C:\Users\RUNNER~1\AppData\Local\Temp\jdk.jpackage6105194214114584024\images\win-msi.image\krystalbull\app\classes\com\krystal\bull\gui\SettingsMenu$anon$5.class'.
You can see more instances of this error in the build log here:
https://github.com/bitcoin-s/krystal-bull/runs/2697961906?check_suite_focus=true#step:8:11
My question is this:
What are the limitations in the windows file system or the program candle.exe that prevent the pre-processor from reading Scala files that include things like $anon$
Is there a valid syntax guide somewhere?
Will I need to manually rename all of these files?

Websphere works when run but fails when debug

I'm using Intellij with WebSphere 8. When I run from within IDE the server works normally. When I try to run it in debug mode however, it fails with the following error:
C:\IBM\WebSphere\AppServer\profiles\AppSrv01\bin\generated_websphere_server_start_script.cmd
C:\IBM\WebSphere\AppServer\java\bin\java -Dfile.encoding=windows-1252 -classpath "C:\IBM\WebSphere\AppServer\runtimes\com.ibm.ws.admin.client_8.5.0.jar;C:\IBM\WebSphere\AppServer\plugins\com.ibm.ws.security.crypto.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 13.1.3\plugins\webSphereIntegration\lib\webSphereIntegration.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 13.1.3\plugins\JavaEE\lib\javaee-impl.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 13.1.3\lib\openapi.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 13.1.3\plugins\webSphereIntegration\lib\specifics\webSphereClientImpl.jar" com.intellij.javaee.oss.process.JavaeeProcess 62847 com.intellij.j2ee.webSphere.agent.WebSphereAgent
Error: JDWP agent already loaded - please check java command line options
[2014-08-11 01:58:59,248] Artifact x.ear: Server is not connected. Deploy is not available.
JVMJ9TI064E Agent initialization function Agent_OnLoad failed for library jdwp, return code -1
Detected server admin port: 8880
JVMJ9VM015W Initialization error for library j9jvmti26(-3): JVMJ9VM009E J9VMDllMain failed
Detected server http port: 9080
Disconnected from server
I tried almost everything I have no idea what the problem is. I did google it for several hours with no luck.
Do anyone know what this all is about and how can it be fixed?
Here is my server configuration:
Solution 1
unchecked the Pass environment variables check box and restart the server in debug mode. it should work properly.
Run --> Edit Configuration --> websphere server --> Startup/Conections tab
select debug, you will see the Pass environment variables check box. it need to be unchecked for debug to work.
Solution 2
if debug mode of websphere is working in eclipse and not working in intellij that reason i found out is debugging serverice on the websphere is already started and intellij is again trying to start the debggin service. so stop the service from websphere console (Servers > Server Types > WebSphere application servers > [serverName] > Debugging Service) and all the default configurations in intellij should work.
I had the same problem. Finally I figure it out. I hope this solution helps. I'm using Intellij idea 2019.1.3.. and WebSphere 8.5.5.13
check WebSphere start server script find debug env variable name (in my script (WebSphere\AppServer\bin\startServer.bat)
WAS_DEBUG)
add same debug option name to intelliJ IDEA
run- debug configuration environment tab.
Since default env variable is debug and cannot be overridden
check pass env variables
add WAS_DEBUG option as I did
or you can update WAS_DEBUG as DEBUG (same as default in IntelliJ) in server startup.bat script.
Both should work.
Pretty old, but i was able to experience too!
They will try to fix this now in: https://youtrack.jetbrains.com/issue/IDEA-193580
firstly, sorry if the translation is not good, I am Brazilian and I would like to share the solution I made.
First: Locate the file "startServer.bat in "WebSphere\AppServer\bin" and open it with any text editor.
Second: Search for the second incidence of the word "WAS_DEBUG" and replace it with "DEBUG", as shown in the image...
Third: In Intellij, go to the server settings and in the "Startup/Connection" tab, select debug.
Fourth: Now uncheck the "use default" checkbox in "Startup script" and point to the location of the "startServer.bat" file in the webSphere directory.
Fifth: Right on the left side of the checkbox, there is an option to enter a parameter. Click and a "Program Arguments" field will appear. Enter the server name, in my case it was "server1".
Okay, now just test
enter image description here
enter image description here

ZMQ - libzmq.so.3: cannot open shared object file: No such file or directory

I'm trying to embed zeroMQ in my app, I followed this guideline to install ZMQ, so till here everything works fine.
I have this line of code in my app:
ZMQ.Context m_context = ZMQ.context(1);
but above line of code raise below exception:
Exception in thread "main" java.lang.UnsatisfiedLinkError: /tmp/libjzmq-812339378390536247.lib: libzmq.so.3: cannot open shared object file: No such file or directory
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary1(ClassLoader.java:1939)
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1864)
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1825)
at java.lang.Runtime.load0(Runtime.java:792)
at java.lang.System.load(System.java:1059)
at org.zeromq.EmbeddedLibraryTools.loadEmbeddedLibrary(EmbeddedLibraryTools.java:136)
at org.zeromq.EmbeddedLibraryTools.<clinit>(EmbeddedLibraryTools.java:22)
at org.zeromq.ZMQ.<clinit>(ZMQ.java:38)
at com.castaclip.verticals.Messenger.<init>(Messenger.java:125)
at com.castaclip.verticals.PushMessenger.<init>(PushMessenger.java:30)
at com.castaclip.verticals.pushserver.App.setup(App.java:60)
at com.castaclip.verticals.pushserver.App.main(App.java:41)
The error is exactly pointing to this line.
P.S: its a little bit difficult to fully explain this question.. if you have any question plz let me know. thanks.
If you've successfully built libzmq and jzmq in that order, I would run:
$ sudo ldconfig
to update the system library cache. Then I would check to see if LD_LIBRARY_PATH is defined like Raffian mentioned, or set your library path explicitly to something like:
$ java -Djava.library.path=/usr/lib:/usr/local/lib
Finally I tried to figure out the problem.
I was using zeromq-2.1.10 and this was part of the problem.
So I installed zeromq-3.2.3 from the source and problem resolved.
I encountered a mystifying instance of this message when I:
# java -Djava.library.path=/usr/hf/zmq/lib/ -cp '/usr/hf/lib/*:.' com.zmqtest.MA
Exception in thread "main"
java.lang.UnsatisfiedLinkError: /usr/hf/zmq/lib/libjzmq.so:
libzmq.so.3: cannot open shared object file: No such file or directory
which was fixed with a solution that makes no sense at all to me:
# LD_LIBRARY_PATH=/usr/hf/zmq/lib/ java -Djava.library.path=/usr/hf/zmq/lib/ -cp '/usr/hf/lib/*:.' com.zmqtest.MA
wierd.

Writing CGI script in Java

I trying to figure out how to write CGI scripts in Java.
I followed this examples -> http://www.javaworld.com/jw-01-1997/jw-01-cgiscripts.html?page=1
It provide cgi_lib.java, hello.html and hello.java
Everything seems fine, but in the html part.
The action is pointed to cgi_lib/hello.cgi
There's no cgi provided. So I tried with cgi_lib/hello.java, and it print the entire source code in the hello.java.
Then i tried to edit the hello.java extensions into hello.cgi, and tried again.
The browser returns me error 500.
What is the problem?
Is it that, there's some specific method to compile the hello.java into hello.cgi?
The script in hello.cgi is different from hello.java?
Please help.
Thank you.
UPDATE
I added hello.cgi
#!/bin/sh
java -Dcgi.content_type=$CONTENT_TYPE -Dcgi.content_length=$CONTENT_LENGTH - Dcgi.request_method=$REQUEST_METHOD -Dcgi.query_string=$QUERY_STRING -Dcgi.server_name=$SERVER_NAME -Dcgi.server_port=$SERVER_PORT -Dcgi.script_name=$SCRIPT_NAME -Dcgi.path_info=$PATH_INFO hello
So is the $CONTENT_TYPE, $CONTENT_LENGTH,... remain the same? Or should I enter something?
Just to make things clearer.
I put the the cgi_lib, hello.java and hello.cgi in the C:\xampp\cgi-lib
And the hello.html in C:\xampp\htdocs\test
When I tried to connect it returns me this
Server error!
The server encountered an internal error and was unable to complete your request.
Error message:
couldn't create child process: 720002: hello.cgi
If you think this is a server error, please contact the webmaster.
Error 500
localhost
Apache/2.4.3 (Win32) OpenSSL/1.0.1c PHP/5.4.7
I had check the httpd.conf in c:\xampp\apache\conf and configure according to this
LoadModule cgi_module modules/mod_cgi.so
ScriptAlias /cgi-bin/ "C:/Program Files/Apache Group/Apache/cgi-bin/"
Seems to be already enable.
So whats the problem right now?
The contents of hello.cgi is shown at the top of page 2 of the article.
#!/bin/sh
java -Dcgi.content_type=$CONTENT_TYPE -Dcgi.content_length=$CONTENT_LENGTH -Dcgi.request_method=$REQUEST_METHOD -Dcgi.query_string=$QUERY_STRING -Dcgi.server_name=$SERVER_NAME -Dcgi.server_port=$SERVER_PORT -Dcgi.script_name=$SCRIPT_NAME -Dcgi.path_info=$PATH_INFO hello
The article says that you will execute the java jar from within a cgi script. You need to make sure that this script is set up to execute your jar file by making the correct calls to the java executable with your hello.jar file as the parameter. Also make sure your web server is configured correctly to allow the execution of cgi scripts.
You also have to chmod of your cgi and java files so others can execute them.

Categories