Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 2 years ago.
Improve this question
It happened that someone filled form and attached file with virus. Our application only checks file extension and size and nothing else is validated. Uploaded files could be checked with some antivirus or something...
What is the best solution here?
I'm using https://github.com/philvarner/clamavj. Download ClamScan.java and ScanResult.java.
and then I have somelike this this to call it (untested):
protected ScanResult.Status virusScanFile(File file) {
ClamScan clamScan = new ClamScan(clamAVHost, clamAVPort, clamAVTimeout);
ScanResult scanResult = null;
if (clamScan.ping()) {
try (InputStream inputStream = new FileInputStream(file)) {
scanResult = clamScan.scan(inputStream);
} catch (FileNotFoundException | IOException e) {
logger.error(e.getStackTrace());
}
} else {
throw new RuntimeException("Could not scan file as ClamD did not respond to ping request!");
}
ScanResult.Status scanResultStatus = null;
if (scanResult != null) {
scanResultStatus = scanResult.getStatus();
}
return scanResultStatus;
}
If you need to install ClamAV on windows for development purposes then this may work for you:
Download http://oss.netfarm.it/clamav/ which contains clamd.exe;
Download http://www.clamwin.com/ which is the Windows version of ClamAV and contains the virus definitions updater (freshclam.exe);
Install both applications as normal;
Copy clamd.conf to C:/Clamav and edit as follows:
LogFile C:\Program Files (x86)\ClamWin\bin\clamd.log
DatabaseDirectory C:\ProgramData\.clamwin\db
Open a cmd prompt with Administrator priviledges and 'cd' to the
Clamav folder where you will find clamd.exe;
type "clamd.exe --install" (no quotes);
Open the Windows services and set "ClamWin Free Antivirus Scanner
Servce" to autostart.
Otherwise just connect to a Linux install via the clamAVHost and clamAVPort parameters, the values of which you will need to define.
You could probably use something like Clamscan and use their command line tool to point to any file that you want or include a script in your application that does the checking.
clamscan OPTIONS File/Folder
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I want to open MATLAB File(.m) using Java
I know MATLAB API.
It's a shame, but I don't know how to use it. How can I open MATLAB File(.m) using MATLAB API?
What should I do?
If possible, can you show an example code?
Thank you.
calling matlab function in java
1-first, add matlab as an Environment variable in windows.
in win10: search Environment variable, Edit environment variable, system variables, path, edit, new, ...
add [matlabroot]/bin/win64 to the path variables.
2-import matlab engine in your java class and use MatlabEngine and its functions :(eval,evalAsync,feval,...) :
import com.mathworks.engine.*; //import engine
public class javaEvalFunc {
public static void main(String[] args) throws Exception {
try{
MatlabEngine eng = MatlabEngine.startMatlab();
eng.evalAsync("[X, Y] = meshgrid(-2:0.2:2);");
eng.evalAsync("Z = X .* exp(-X.^2 - Y.^2);");
Object[] Z = eng.getVariable("Z");
eng.close();
}catch(Exception e){}
}
}
3-to call a specific .m routine, call it with its full path with eval,...
eng.eval("c:\temp\myroutine");
I have a program that is emulating a simple distributed file system. I need to be able to open a file locally using emacs in the terminal window to read or write. I am using Runtime.exec() on an String[] to represent the commands. If I use "emacs" I can get the machine I'm currently using to open an emacs window (this doesn't help for any other machines running the Client). However, I have been unable to resolve how to have emacs open within the terminal using this process. I've tried adding the parameter "-nw" but it does not seem to respond. This is what I'm currently trying to use:
boolean bool = execute("emacs", "-nw", cachedFile);
private boolean execute(String com, String id, String fileName) {
String[] commands = new String[3];
commands[0] = com;
commands[1] = id;
commands[2] = fileName;
try {
Process p = Runtime.getRuntime().exec(commands);
int blah = p.waitFor();
} catch(Exception e) {
e.printStackTrace();
return false;
}
return true;
}
If I drop the "-nw" then I can get only the machine that I'm working on locally to open a separate window for the GUI emacs. This is not helpful because I need other remote machines (that I'm connected thru with terminal) to be able to view or edit the file and X does not work since I'm not at those machines to see the new window.
I know for sure that "emacs -nw filename" is the proper command because if I manually type this into the terminal then it'll open emacs. However, I want the Runtime.exec() to manage this request.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How can I run a file (like an exe program) using java? I have searched up how to open a file (even on oracle docs) to no avail. Is there a class to help me with this? I tried
try
{
Runtime rt = Runtime.getRuntime() ;
Process p => rt.exec("Program.exe") ;
InputStream in = p.getInputStream() ;
OutputStream out = p.getOutputStream ();
InputSream err = p.getErrorStream() ;
p.destroy() ;
}
catch(Exception exc)
{
}
But it didn't work
Try this
Runtime.getRuntime().exec("c:\\program files\\test\\test.exe", null, new File("c:\\program files\\test\\"));
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
How can I play module music (.it, .mod, .xm or .s3m files) in a Java application? (Standard Java for desktop machines)
I tried looking at the open source JavaMod, but it had too many files so I didn't know where to start, and the pre-built .jar file wouldn't even run on my machine.
Add javamod.jar to the application class path and try with this code:
public static void main(String[] args) {
try {
Helpers.registerAllClasses();
File music = new File("c:\\test3.XM");
Properties props = new Properties();
props.setProperty(ModContainer.PROPERTY_PLAYER_ISP, "3");
props.setProperty(ModContainer.PROPERTY_PLAYER_STEREO, "2");
props.setProperty(ModContainer.PROPERTY_PLAYER_WIDESTEREOMIX, "FALSE");
props.setProperty(ModContainer.PROPERTY_PLAYER_NOISEREDUCTION, "FALSE");
props.setProperty(ModContainer.PROPERTY_PLAYER_NOLOOPS, "FALSE");
props.setProperty(ModContainer.PROPERTY_PLAYER_MEGABASS, "TRUE");
props.setProperty(ModContainer.PROPERTY_PLAYER_BITSPERSAMPLE, "16");
props.setProperty(ModContainer.PROPERTY_PLAYER_FREQUENCY, "48000");
MultimediaContainerManager.configureContainer(props);
URL modUrl = music.toURI().toURL();
MultimediaContainer multimediaContainer = MultimediaContainerManager.getMultimediaContainer(modUrl);
Mixer mixer = multimediaContainer.createNewMixer();
mixer.startPlayback();
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.exit(3);
} catch (Exception e) {
e.printStackTrace();
}
}
Works fine for me. But I would also suggest to try running javamod stand-alone in order to make sure it works on your system.
To get JavaMod to run on your machine, you have to run it with the java -jar command.
This screenshot illustrates how to do this:
Whoa, cool that JavaMod even exists to play old school modules in Java. I doubt you're going to find much better support these days. Honestly you're best bet is probably going to be to figure out how to call into that library, or just use the source code directly. You'll likely have to look through the code to determine how to integrate with it. By the way, I'm able to run it fine by just double clicking on the .jar file (I'm using Java 6 Update 24 on Windows 7).
I'm trying to figure out how to open the system preferred editor for a given file.
Say, we have a file manager, written in Java. User goes to folder and sees the list of files. And, for example, there is a file Icon.jpg. User double clicks on the filename and file opens in system's preferred editor (i.e. Gimp). The main issue is - how to do that?
We can do Runtime.getRuntime().exec("something file"), but this way you should know which program is preferred in user environment. But how?
We also are able to do Desktop.getDesktop().edit(File file), but this way we cannot track process and aren't able to know then this child process is closed. Other issue - function doesn't work on linux (at least on Ubuntu 8.10). There is also Desktop.getDesktop().open(File file), but it forces to open file viewer, instead of system viewer for that file type.
I am searching for a solution all week, but didn't got any suitable and generic one. Do you know the other approaches to this question? For my project it would be enough if it would work on Windows+Linux+Mac.
Thank you for your answers and advices.
Edit on 2009-02-08 23:04
Other suggestion: can I force "application selection" window in Windows and in Linux, as in Mac with "open file"? For example, then you trying to open file, you are being asked to choose application from list of system preferred ones? (something like "Open with..." in Windows explorer). Do you know?
Seems that if you can't use java.awt.Desktop you have to distinguish between the OSes:
Windows:
RUNDLL32.EXE SHELL32.DLL,OpenAs_RunDLL <file.ext>
Linux:
edit <file.ext>
Mac:
open <file.ext>
HTH. Obviously, that is not very portable...
Check out the java.awt.Desktop object. In your case, you want to invoke edit()
If you want to ensure that a given platform supports this call, then you can do something like the following (I have not tested this code):
public boolean editFile(final File file) {
if (!Desktop.isDesktopSupported()) {
return false;
}
Desktop desktop = Desktop.getDesktop();
if (!desktop.isSupported(Desktop.Action.EDIT)) {
return false;
}
try {
desktop.edit(file);
} catch (IOException e) {
// Log an error
return false;
}
return true;
}
This isn't cross-platform, but on Mac OS X you can do
Runtime.getRuntime().exec("open filename");
The open(1) executable uses LaunchServices to pick the right program to execute, and then uses that to open the file named filename.
This will work in windows
Runtime.getRuntime().exec( "CMD /C START filename.ext " );
For JavaFX applications, we can use HostServices. This question covers how to use HostServices. This should work on Ubuntu (tested)/Windows (not tested) and Mac (not tested).
import java.io.File;
import java.io.IOException;
public class App extends Application {
}
File file = new File("/your/file/path");
HostServices hostServices = getHostServices();
hostServices.showDocument(file.getAbsolutePath());
getHostServices() is a method of JavaFX Application class.