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).
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");
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 2 years ago.
Improve this question
Please suggest How can I improve my question
I ran my code but it throws cannot find main class error. I know this error comes when name of class with main method is different from file name. I tried to solve this error for an hour. I need help.
CountRows.java
import java.io.*;
import java.sql.*;
public class CountRows
{
public static void main(String args[])
{
System.out.println("Count number of rows in a specific table!");
Connection con = null;
int count = 0;
try
{
//Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctutorial","root","dics");
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Execution
>javac -classpath "e:\softwares\java\jar files\mysql-connector-java-8.0.19.jar" CountRows.java
E:\user\java\jdbc\test>java -classpath "e:\softwares\java\jar files\mysql-connector-java-8.0.19.jar" CountRows
Error: Could not find or load main class CountRows
Edit: Although the solution works but I still don't understand what was wrong with my way of executing code
Your Code is working perfectly fine there is no issue in it.
I suggest you set classpath initially before using javac and java tools.
Set your classpath via CMD
set classpath="<EXTERNAL_JAR_FILES_PATH>"
I hope this might help you. If you are still facing the same issue you can ping me back. I will be happy to help you.
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
This question already has an answer here:
Java7 WatchService - Access Denied error trying to delete recursively watched nested directories (Windows only)
(1 answer)
Closed 7 years ago.
I'm using the new feature of Java7 for watching directories for changes (as it is described in the following tutorial: http://download.oracle.com/javase/tutorial/essential/io/notification.html )
The watching itself works (nearly) without problems.
More or less I have two issues and I hope some of you have an idea how to handle it.
After a folder is added to watch, the VM has access to it and it doesn't release it. This should not be such a big issue. But if you're creating a directory in another directory and in this one more. (e.g. you create in directory c:\tmp\a the directory b and in b the directory c (c:\tmp\a\b\c)) it is not possible to delete the directory a, because of the access of the VM to b and c. If you want to delete all of the folders (e.g., with your Windows Explorer) you have to delete c first, then b and then a. It's strange, but this way works (though is inconvenient)
This issue seems to be a result of the first one.
If many changes in a occur sometimes (more or less) I get an exception which tells me, the newly created folder is used by another process and it is not possible to access it. It's strange, because I think this should not matter to get a notification. And because it is not completely clear when exactly this exception is thrown.
Do you have an idea how to make it possible to not have the lock or at least to let the user the ability to delete such a directory structure in the classical convenient way?
Well I've tried the code showcased in the link you posted, doing the same things you're trying to do:
In my c:\temp directory I create programatically a c:\temp\a dir and then a c:\temp\a\b directory:
File startDir = new File("c:\temp");
if(!startDir.exists()) {
startDir.mkdir();
}
File aDir = new File("c:\\temp\\a");
File bDir = new File("c:\\temp\\a\\b");
if(!aDir.exists()) {
aDir.mkdir();
}
if(!bDir.exists()) {
bDir.mkdir();
}
Then I add watchers to the "a" and "b" directories:
public static void watch(final File dir,final WatchService watcher) {
Path path = dir.toPath();
try {
final WatchKey bDirWatchKey = path.register(watcher, StandardWatchEventKinds.ENTRY_MODIFY);
new Thread(new Runnable() {
public void run() {
System.out.println("Watching: "+dir.getName());
while(true) {
try {Thread.sleep(1000);} catch (InterruptedException e) {}
List<WatchEvent<?>> events = bDirWatchKey.pollEvents();
for(WatchEvent<?> event:events) {
System.out.println(dir.getName()+" event: #"+event.count()+","+event.kind()+" File="+event.context());
}
}
}
}).start();
} catch (IOException x) {
x.printStackTrace();
}
}
This works ok, if I modify files in "a" or "b" I get the corresponding console output.
It's true that with Windows Explorer (on a Windowx XP machine) I cannot delete a watched directory (it tells me I don't have access rights). I can however delete it with other tools such as Total Commander. I can even delete then from the Windows command line with rd c:\temp\a\b. I think this is more of an issue with Windows Explorer than with Java...
with regard to your 2nd problem, if you are getting the exception " the newly created folder is used by another process and it is not possible to access it". You can try using Atomic Operations in your code. this will help the operations to be not interrupted and MAY solve your problem.
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.