Java Project admin privileges windows 7 - java

I am building a swing application (a file explorer) that has to copy/move files/folders around. When I try to copy to some folders such as Program Files, it throws an exception (access denied). I can solve by running NetBeans as administrator.
Is there anyway I can give admin rights to my project only, without running the whole Virtual Machine as admin?

You could, for a really horrible non-cross-platform method, use VBScript's .ShellExecute and Runtime.exec to force a program to run as an administrator.
For brevity's sake, I have created a simple program available at
https://dl.dropbox.com/u/26746878/Misc/JavaElevated.zip
It can be run with java Launcher.
From NetBeans, I'd assume you'd somehow get it to run the Launcher as the main class instead of the main Program.

Is there not a command line startup, I'm sure there is and that there is a guy(ette) out there who would write it out for us in about 10 hot seconds. Windows 7/64

Related

Javaw.exe application not opening, WIndows 10 Pro Surface Pro 4

I have a Java application from one of our suppliers. After installing the application as Admin, I try to open the application as Admin, but nothing happens. I have gone to where the program icon points to, and it goes to a Javaw.exe file:
"C:\Program Files (x86)\Infra12\jre\bin\javaw.exe" -jar Infra12.jar
If I run this Javaw.exe file directly as Admin, nothing still happens. By nothing, I mean there are no windows that open at all.
I have run sfc /scannow and it found nothing.
I didn't have Java itself installed, so I thought maybe that was the issue. So I installed Java (Java 8 Update 191), uninstalled the Java application then reinstalled it, still nothing happens when I try to open it.
I can't go back to the supplier, as they don't support the software any more - it is used to communicate with infrastructure equipment.
Any ideas how I can get this application to work?
Edit
Not sure if this makes a difference, seems to refer to some variables - maybe that can help with opening it?
This is because javaw points to the Java Virtual Machine. If you want to run the application - find the .jar it uses and then double-click that. Or use javaw to run said application:
[WIN] + [R]
Type "cmd"
Enter: javaw -jar path-to-your-jar-file
javaw by itself will do nothing.
EDIT: You need to use the -jar option.

Java program won't run on double click

What the title says really. I've tried exporting it to a Runnable Jar File, checked the Manifest.MF, and tried running it with Java multiple times but with no luck. However, it does work when I navigate to the file using the command prompt and launch is using java -jar Name.jar. This shows it works but I just can't get it to launch by double clicking.
I guess you're trying to launch a commandline-app. This kind of application can be launched by double-clicking aswell. There's only one problem: Java doesn't create a commandline-window by default and instead uses the commandline of the parent-process of the JVM, which in case of double-clicking doesn't own a console-window. In other words: the output to the console gets lost somewhere in the depths of your OS and the JVM and the program hangs as soon as any input is expected.
(Assuming your on Windows OS), if you right-click on it, do you see the option 'Open with' and then 'Java (TM) Platform SE binary', or something like that? And if you opt to open with that, does it execute?
If so, then you've probably set .jar files to open with a different application by default. For example, I have my computer set up to open .jar files with jd gui as the default application.

Running a JAVA program as a scheduled task

I am trying to run a simple JAVA program once per day on a Windows 7 machine.
My code runs fine inside NetBeans. If I do a clean and build it suggests this:
C:\Program Files\Java\jdk1.7.0/bin/java -jar "C:\Users\User1\Documents\NetBeansProjects\Facebook\dist\Facebook.jar"
This does not work from the DOS prompt of course because of the space between program and files so I do this:
C:\Program Files\Java\jdk1.7.0/bin/java -jar "C:\Users\User1\Documents\NetBeansProjects\Facebook\dist\Facebook.jar" -jar "C:\Users\User1\Documents\NetBeansProjects\Facebook\dist\Facebook.jar"
This works from the DOS prompt.
I now create a task in Windows Scheduler to run:
C:\Program Files\Java\jdk1.7.0/bin/java
with arguments:
-jar "C:\Users\User1\Documents\NetBeansProjects\Facebook\dist\Facebook.jar"
When I then run it, all I see is a DOS box flashing up for a second. I expect the code to take about 30 secs to run. The code should persist data to a database and no updates happen.
The code also uses java.util.logging so I should see log entries and I don't.
I strongly suspect that I am not running the JAVA command properly or that there's a bad classpath issue that it present when running via Scheduler that isn't there when running from the DOS prompt.
Help would be appreciated. If you've seen this before and can sort it that would be great. If you can tell me how to get a meaningful error trace from Scheduler than that would also be really helpful.
Thanks!
I Think that you could create a simple batch script that will launch your program in this way :
#echo off
REM Eventually change directory to the program directory
cd C:\Users\User1\Documents\NetBeansProjects\Facebook\dist\
REM run the program
"C:\Program Files\Java\jdk1.7.0\bin\java.exe" -jar "C:\Users\User1\Documents\NetBeansProjects\Facebook\dist\Facebook.jar"
Copy it into the notepad and save as java_script.cmd and then schedule this script instead of the program directly.
I solved it after changing all fonts' references to "SansSerif"
I was using Jasper Reports inside Java to create a PDF file. It was working fine when I double click the batch file or Scheduler with Windows Server 2003 but not working with the Scheduler of 2008.
I tried many different things nothing worked so I though Could it be that Windows Server 2008 is blocking the access?.
Now is working perfect. So, if you are having problems check the references to anything you are using.
The scheduler will run under a different user unless you specify what user to run as. If it isn't running as your user then it won't be able to write to your directories.
The real problem to the original question is a java installation issue on Microsoft systems. Java jre installs into Program Files\java. The executable (java.exe) is only installed in that java\bin directory. Running from the command line, the os looks in the proper location for the java.exe. Running from other MS tools (such as VBA Excel or in this case TaskScheduler), it does not!
You can see that TaskScheduler is looking in the wrong place by viewing the tasks history in the TaskScheduler tool. Double click on some of the history events and one will list the action and return code. The action will show that the TaskScheduler is trying to run
"C:\Windows\system32\java.EXE"
So, copy java.exe from the java\bin directory into the place where the scheduler is looking, and now it will work.
Or update your task and provide the full path to java.exe.
You can also update the environment system path to look for java in the java\bin directory, but that has to apply to all users and sometimes this is faulty as well.

mkdirs not working in windows 7

I made a small java app that copies a directory from a CD to the HD. I made the program using Windows Vista and it worked, but when i ran it in Windows 7, it fails.
The main problem is that a folder inside the Program Files folder needs to be created.
I used DestinationFolder.mkdirs(), but it fails creating it
This is the java code:
public void Install_App()
{
File srcFolder = new File(System.getProperty("user.dir") + "\\WINDOWS");
File destFolder = new File("C:\\Program Files\\test1\\test2\\");
if (srcFolder.exists())
{
try{
if(!destFolder.exists())
{
destFolder.mkdirs();
}
copyFolder(srcFolder,destFolder,1);
}catch(IOException e){
e.printStackTrace();
JOptionPane.showMessageDialog(null, e.toString());
error=true;
System.exit(0);
}
} else
{
JOptionPane.showMessageDialog(null, "Error. Source Directory doesn't exist.");
error=true;
};
}
... and then there is a copyfolder function that copies the files with inputstream and outputstream.
The problem is that the folder is never created. My login user is an administrator. And as i said, it worked in Vista.
Could you help me, please?
Thanks.
The thing is that i created this app in java to run it in Windows and Mac.
In Windows it should autorun with and autorun.inf like this:
[autorun]
OPEN=java_app.bat
then this bat will run this:
#echo off
start javaw -jar "java_app.jar"
EXIT
so how can i modify it to run it as administrator automatically?
The main idea of this java app is simplify the process of install & use an external application no matter which OS are you using. If I have to ask the user to run it as admin it will loose it's sense (of been simple of use).
I am guessing you are running your code as regular user.
Writing into Program Files directory as a regular-user is by default blocked by UAC under Windows 7. That's why your Java code fails to create directories.
Try running your Java code from a privileged shell. You can have one by Start > [type cmd] > [right-click on 'cmd.exe' and select "Run as administrator"]. Now, run your compiled code with java -jar or java -classpath from the administrator command prompt. It should work now.
Automating the UAC prompt:
You need to create a manifest file as described in detail at [1] and [2] to let Windows/UAC know that your program would need elevated privileges.
Also check this [3] utility called elevate that would spawn your program as child process while handling the UAC permission requests all being made from the parent (elevate) program itself.
[1] [http://msdn.microsoft.com/en-us/library/aa511445.aspx][2]
[2] [http://msdn.microsoft.com/en-us/library/bb756929.aspx][3]
[3] [http://www.wintellect.com/cs/blogs/jrobbins/archive/2007/03/27/elevate-a-process-at-the-command-line-in-vista.aspx][4]
This is all the permission problems. I have the same problem on my machine. Nothing is wrong with your java code. I tried to create folder using command line and got "Access Denied".
C:\Users\alexr>mkdir "C:\Program Files\mytest"
Access is denied.
So, the solution is whether to create folder in other location or run as administrator. As #Alex K. aready said, refer to this post to learn how to get such permissions.
Windows 7 Create Folder in "Program Files" failing in C# code even thought I have admin rights!
You do not have the proper privileges to create directories in Program Files. You must start the application with administrative privileges.
An important thing to learn is that when you are developing your applications you should never write them to save/modify data inside Program Files; instead they should either write to AppData our My Documents.
Modifying files in Program Files has been severely deprecated ever since Windows Vista, and even earlier than that. You should try and follow this rule from the start, or it means headaches to rewrite your entire application if you ever want to publish it online.

Java programme works perfectly in NetBeans, but exits unexpectedly when trying to run

I've run into a weird problem today. I'll try to describe the steps I'm making as detailed as possible.
I've created a Java application in NetBeans. The application runs without exceptions when running within NetBeans by clicking Run Main Project. The total duration of a complete run is about 1min.
When building the project I see the following option in the output window of NetBeans:
To run this application from the command line without Ant, try:
java -jar "C:\java_libraries\PrxJobCommunicator\dist\PrxJobCommunicator.jar"
So i open the command prompt (Windows 7), enter this command + supply a mandatory integer argument and press enter
The application starts running, runs for a couple of seconds and exits without any message whatsoever.
the application writes to a text file at different points in the code, so I'm able to trace the output up to the point when it stops running. I've deliberately put some extra calls to the file writer method in order to find where the programme continues/exits but to no avail. Seemingly the programme simply stops running on a certain line and that's it.
I really don't know what the problem can be: the application runs fine in NetBeans and fails without any message whatsoever when starting from the command line.
Shall I check anything special in NetBeans? Do you know of any special technique to debug a jar file if it's started from the command line?
Any clues are welcome.
Thanks, Andras
I suspect that there is a difference in the classpath when the app is run from NetBeans, and when it is run from the command line.
There must be some kind of exception that is being thrown, have you got an outermost
catch (Exception ) block in the main method?
You could try to debug the application using "remote debugging", although you won't be debugging over a network, you can still use this method to catch the breakpoint started at the command line in NetBeans.
Here's an article that explains how to do remote debugging with NetBeans http://manikandanmv.wordpress.com/2009/09/24/debugging-java-applications-with-netbeans/
Netbeans by default uses your project folder as the working folder. It means when you run your program from console you have to CD C:\java_libraries\PrxJobCommunicator
then run java -jar "C:\java_libraries\PrxJobCommunicator\dist\PrxJobCommunicator.jar"
If above fails check the java version used in your command prompt. java -version
It has to be same as the one used by netbeans.
Finally the library path. Are all the dependent jars placed in the dist folder?

Categories

Resources